我目前正在努力实现样式和模板的字典,以便可以动态应用到我的应用程序中。在实现这种新的动态行为之前,我有几个资源字典,每个控件都有一个,我将它们合并在App.xaml中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsDictionary.xaml"/>
<ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
现在,我希望我的应用程序有样式,所以我决定将之前所有的资源合并为一个名为"MyFirstTemplates"的新资源,并将仅此字典添加到App.xaml。
新的字典"MyFirstTemplates.xaml":
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">"
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsDictionary.xaml"/>
<ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
新的 App.xaml 文件:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyFirstTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Window}"/>
</ResourceDictionary>
</Application.Resources>
注意: Window 的默认样式是为了修复 WPF 4 的错误,请参见添加合并字典到一个合并字典中。现在我已经做出了这个改变,我不能再像以前那样在“ControlsTemplateDictionary.xaml”中使用“ColorsDictionary.xaml”中的颜色资源作为StaticResource。如果我改回将这些文件合并在 app.xaml 中,一切正常。为了让它工作,我必须将这些StaticResource 改为 DynamicResource。你有任何想法为什么这不再起作用吗?
谢谢 :-)