WPF中的Generic.xaml, theme以及custom control
原文:WPF中的Generic.xaml, theme以及custom control
在Visual Studio中创建自定义控件时,所有控件都将添加到/Themes/Generic.xaml。
最近做的项目用了个漂亮的开源UI库,结果项目临近结尾发现要支持.Net 3.5, 然而那个UI库却最低支持4.0。欲哭无泪,最后决定拿掉那个库,自己改改style得了。深刻的教训。
作为程序员一般都比较害怕搞界面,这下硬着头皮上,折腾折腾Blend,抄抄改改各种style,弄着弄着居然能看懂个大概了。最后在自己建立的UI库项目中如何组织资源有了一些总结,进入正题:
“Generic.xaml” 早有耳闻,不清楚究竟有什么作用。这次Google之,stackoverflow上早已有人发问。What is so special about Generic.xaml?
第一个答案清晰明了。
Every Control in WPF has a default Style that provides, among other things, the Control's default
ControlTemplate. WPF looks for the default style in a special resource dictionary in the Themes folder in the same assembly as the control. The key for the default style is provided by theControl.DefaultStyleKeydependency property, the default value of which is overridden in each sub-class of Control.The name of the resource dictionary depends on the current Windows theme e.g. on Vista using the Aero theme, the dictionary is called Aero.NormalColor.xaml, on XP using the default theme it is Luna.NormalColor.xaml. If the style is not found in the theme dictionary, it looks in Generic.xaml i.e for controls whose look doesn't depend on the theme.
This only applies to any custom controls you have defined i.e. classes derived from Control, directly or indirectly. You can change the default style for a standard control by deriving from it and calling
DefaultStyleKeyProperty.OverrideMetadatain the static constructor, but you then have to supply the full style including ControlTemplate.Note that you can tell WPF to look in an external assembly for your default style by using the ThemeInfo attribute. The external assembly must be named
<YourAssembly>.<ThemeName>.dll e.g. PresententationFramework.Aero.dll.
讲解了Generic.xaml的来龙去脉。顺便提了如何使用DefaultStyleKeyProperty.OverrideMetadata 来改变WPF查找一个control的默认style时用的类型。还提到了 ThemeInfo.
下面这篇文章,Structuring Your XAML Sources,代码演示的非常清晰。通过它还能了解创建一个Custom Control的典型做法,很有帮助。总体来说,Generic.xaml在UI Library中的好处就是,使用这个library的项目不用再引用library的resource dictionary,UI Library中定义的custom control也能自动地找到它的默认style。
看文章附带的源代码,以及开源库的代码,发现它们都在AssemblyInfo.cs中有ThemeInfo. 这是必不可少的。
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
WPF中的Generic.xaml, theme以及custom control的更多相关文章
- WPF中动态添加xaml资源文件
一.新建一个资源文件,然后设置其Build Actoin(生成操作)为Resource(资源): 二.在App.xaml.cs的StartUp事件或者是你需要的时机代码段写上如下代码: Resourc ...
- wpf中如何在xaml中绑定cs中类的属性
cs代码:/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWin ...
- ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同
说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml. 可是为什么在其他solution中 ...
- 使用用Generic.xaml加载默认的主题资源
把Resource嵌入到Generic.xaml文件中,并把该文件放到应用程序的Themes主题文件夹下面,这们Generic.xaml文件中的资源就可以被系统识别为默认主题一部分,从而进行使用. 为 ...
- 在WPF中自定义控件(3) CustomControl (下)
原文:在WPF中自定义控件(3) CustomControl (下) 在WPF中自定义控件(3) CustomControl (下) ...
- WPF中通过代码定义模板
WPF中可以再XAML中定义模板,也可以通过C#代码定义模板,通过代码可能更清楚的看清其逻辑,而且代码的好处就是可以随时动态的去操作,而在XAML中定义的一般都是静态的. //控件呈现的显示内容1(这 ...
- WPF关于Generic.xaml
如果需要用到Themes/Generic.xaml作为默认风格资源文件,不要忘了该项目的AssemblyInfo.cs中必须要有以下这段: [assembly: ThemeInfo( Resource ...
- WPF中动态加载XAML中的控件
原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...
随机推荐
- MySQL字符编码问题,Incorrect string value
MySQL上插入汉字时报错例如以下.详细见后面分析. Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1 M ...
- 关于 /etc/zabbix/zabbix_agentd.conf 文件 Hostname 文件的说明
前提 (1) /etc/hosts 文件如下 [root@testdb ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain loc ...
- 9.2 Binder系统_驱动情景分析_服务注册过程
1. 几个重要结构体的引入给test_server添加一个goodbye服务, 由此引入以下概念: 进程间通信其实质也是需要三要素:源.目的.数据,源是自己,目的用handle表示:通讯的过程是源向实 ...
- 修复STS4 server中没有Tomcat的问题(必看,官方推荐,包教包会,国内首发)
版权声明:原创.欢迎转载,转载请注明来源,谢谢. https://blog.csdn.net/qq_41910280/article/details/83279129 修复STS4 server中没有 ...
- spark安装与调试
I---- 1---jdk and scala install ****zyp@ubuntu:~/Desktop/software$ tar xvf jdk-7u67-linux-i586.tar.g ...
- Hbase常见异常 分类: B7_HBASE 2015-02-02 16:16 412人阅读 评论(0) 收藏
1. HBase is able to connect to ZooKeeper but the connection closes immediately hbase(main):001:0> ...
- Swift 中异常抛出和四种异常处理
在Swift中你可以像其他语言一样抛出异常处理异常,今天我们就详细地说说Swift中的异常抛出和处理. 在一开始我们要定义错误或者说是异常,Swift中的一些简单异常可以使用枚举定义,注意这个枚举要继 ...
- 【23.48%】【codeforces 723C】Polycarp at the Radio
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Spring web 工具类 WebApplicationContextUtils
概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...
- MD5工具类--可以直接拿来用不抛异常的MD5Util
public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb ...