ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同
说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml。
可是为什么在其他solution中可以成功显示呢?后来就google学习了一下WPF中加载资源的相关文档,但都是理论性的介绍。对这个问题还是没有多大的帮助。
没有办法只能比较两个solution中的proj有什么不同,打开proj属性,发现application、build、buildEvents...等都一样,后来只能比较assemblyInfo,发现多了
[assembly:ThemeInfo(
......
......
......
)]
一瞬间像找到了答案一样兴奋(虽然没有验证),继续万能的google呀,于是找到了ClassLiary,WPF User Control Libary,WPF Custom Control Libary.
1:先看Class Library和WPF User Control Library,区别在于,
- 增加了4个Reference,PresentationCore,PresentationFramework,System.Xaml,WindowsBase
- AssemblyInfo.cs中多了ThemeInfo的Attribute,如下,
[assembly:ThemeInfo(
ResourceDictionaryLocation.None,
//where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly
//where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)
所以如果要在一个Class Library中新建一个WPF UserControl就需要新建一个WPF UserControl,Visual Stuido会自动为你加入4个Reference,然后手动在AssemblyInfo.cs加入ThemeInfo即可.
[assembly: ThemeInfo(ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
2:然后说说WPF User Control Library和WPF Custom Control Library的区别,结果看下来基本没区别,无非是Custom Control Library默认帮你创建了一个CustomControl继承Control,然后在Themes目录下创建了Generic.xaml。
所以如果新建Class Library后需要创建Custom Control,就需要增加对应的4个Renference,在Add File中无法增加Custom Control,这个VS有点2,只能手动创建Class,然后改为继承自Control,
public class Class1 : Control {
static Class1() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(Class1), new FrameworkPropertyMetadata(typeof(Class1)));
}
}
然后创建目录Themes增加Generic.xaml,确定Generic.xaml的build方式为Page,最后增加ThemeInfo即可。
3:总结一下,原来真的就是assembly中的assembly:ThemeInfo那段代码起到了加载ResourceDictionary的作用。不禁感慨在项目中直接根据源代码摸索真不如好好看一本书呀。
ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同的更多相关文章
- Writing a Reusable Custom Control in WPF
In my previous post, I have already defined how you can inherit from an existing control and define ...
- Recommended Practices for WPF Custom Control Developers
I have always found that there isn’t enough documentation about Custom Control development in WPF. M ...
- WPF中的Generic.xaml, theme以及custom control
原文:WPF中的Generic.xaml, theme以及custom control 在Visual Studio中创建自定义控件时,所有控件都将添加到/Themes/Generic.xaml. 最 ...
- .NET Core学习笔记(2)—— WPF使用UWP Custom Control
自.NET Core 3.0开始,某软加入了对WPF的支持.同时对XAML Islands也做了进一步加强.在.NET Core 3.0之前,我们只能在WPF程序中,通过两种方式有限制地使用Stand ...
- WPF快速入门系列(6)——WPF资源和样式
一.引言 WPF资源系统可以用来保存一些公有对象和样式,从而实现重用这些对象和样式的作用.而WPF样式是重用元素的格式的重要手段,可以理解样式就如CSS一样,尽管我们可以在每个控件中定义格式,但是如果 ...
- WPF快速入门系列(6)—— WPF资源和样式
一.引言 WPF资源系统可以用来保存一些公有对象和样式,从而实现重用这些对象和样式的作用.而WPF样式是重用元素的格式的重要手段,可以理解样式就如CSS一样,尽管我们可以在每个控件中定义格式,但是如果 ...
- WPF/MVVM Quick Start Tutorial - WPF/MVVM 快速入门教程 -原文,翻译及一点自己的补充
转载自 https://www.codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial WPF/MVVM Quick Start T ...
- WPF快速入门系列(1)——WPF布局概览
一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...
- WPF 3D 小小小小引擎 - ·WPF 3D变换应用
原文:WPF 3D 小小小小引擎 - ·WPF 3D变换应用 WPF可以提供的3D模型使我们可以轻松地创建3D实体,虽然目前来看还很有一些性能上的问题,不过对于一些简单的3D应用应该是可取的,毕竟其开 ...
随机推荐
- java keytool证书工具使用小结(转)
Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,包含两种数据:密钥实体(K ...
- Appium+python自动化12-appium元素定位【转载】
前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性
- 在ros下使用tf
tf真是一个好东西,把坐标变换都简化了 首先tf需要有一个broadcaster #include <ros/ros.h> #include <tf/transform_broadc ...
- ASPOSE.WORD 另存为HTML
var fi = new FileInfo(Environment.CurrentDirectory + "\\AE9302C0-AE48-4F4B-8489-6A428D9163C9_AL ...
- UVA 11324 The Largest Clique(缩点+DAG上的dp)
求最大团.和等价性证明有类似之处,只不过这个不是求互推,而是只要a->b,或b->a即可. 同样的,容易想到先缩点,得到DAG,每个节点上保存SCC的点数,相信任意一条由根节点(入度为零) ...
- 51nod 1095 Anigram单词【hash/map/排序/字典树】
1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #325 (Div. 2) Laurenty and Shop 模拟
原题链接:http://codeforces.com/contest/586/problem/B 题意: 大概就是给你一个两行的路,让你寻找一个来回的最短路,并且不能走重复的路. 题解: 就枚举上下选 ...
- AtCoder - 3962 Sequence Growing Hard
Problem Statement Find the number of the possible tuples of sequences (A0,A1,…,AN) that satisfy all ...
- ORACLE查询当前连接的用户信息及操作的SQL语句
ORACLE--查询当前连接的用户信息及操作的SQL语句 select sid, status, v$session.username 用户名, last_call ...