我目前正在研究可以动态应用于我的应用程序的样式和模板字典。在这种“新的”动态行为之前,我有几个资源字典,每个样式控件一个,我合并到 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 的一个错误,请参阅将合并字典添加到合并字典
现在我已经进行了这个更改,我不能再将“ColorsDictionary.xaml”中的颜色资源用作“ ControlsTemplateDictionary.xaml”中的静态资源。如果我改回在 app.xaml 中合并这些文件,一切正常。为了使其工作,我必须将这些StaticResource更改为DynamicResource。你知道为什么这不再起作用了吗?
谢谢 :-)