[Wpf] WPF中的Generic.xaml, theme以及custom control

转载自:https://www.cnblogs.com/lx-nju/p/5276640.html

----------

最近做的项目用了个漂亮的开源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 the Control.DefaultStyleKey dependency 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.OverrideMetadata in 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 (Windows Presentation Foundation) generic.xaml 是用于定义控件样式和模板的文件。要在 generic.xaml 添加事件处理,可以按照以下步骤进行操作: 首先,打开 generic.xaml 文件,可以在 Visual Studio 找到该文件,通常位于项目的“Themes”文件夹。 在 generic.xaml 文件找到你想要添加事件处理的控件模板。一般来说,事件处理会放在控件模板内部的特定元素上,比如一个按钮或者一个鼠标悬停的区域。 找到相应的元素后,可以给它添加事件处理,具体的方法是在该元素的属性添加事件处理程序。比如,可以为该元素的 MouseLeftButtonUp 事件添加一个事件处理程序,代码示例可以是类似这样的: MouseLeftButtonUp="EventHandlerName",其 EventHandlerName 是你想要添加的事件处理程序的名称。 然后,可以在代码编写 EventHandlerName 方法,来处理该事件。需要注意的是,由于 generic.xaml 的控件模板是被多个实例共享的,所以在编写事件处理程序时要考虑到可能会涉及多个实例的情况。 最后,保存并关闭 generic.xaml 文件,然后重新编译项目以应用修改。 总的来说,在 generic.xaml 添加事件处理其实就是找到相应的控件模板元素,为其添加事件处理属性,并在代码编写相应的事件处理程序。通过这样的操作,就可以为控件模板元素添加一些交互行为,从而实现更丰富的用户界面效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值