我有一个库,CommonLibraryWpfThemes,里面有几个Resource Dictionary XAML文件.My Themes/Generic.xml文件包含一个ResourceDictionary.MergedDictionaries声明,它将所有其他文件合并在一起.
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在我的应用程序项目中,我引用了CommonLibraryWpfThemes,并在我的App.xaml文件中显式引用了Generic.xml.
App.xaml - 失败
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
这不起作用.运行我的应用程序时出现以下错误:
System.Windows.Markup.XamlParseException occurred
Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive. Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."
Source="PresentationFramework"
LineNumber=18
LinePosition=13
Run Code Online (Sandbox Code Playgroud)
如果我直接将Generic.xaml的内容放入App.xaml,一切正常:
App.xaml - SUCCEEDS
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary …Run Code Online (Sandbox Code Playgroud) 我似乎无法将合并的字典添加到XAML中的合并字典集合中.
Theme.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
<ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
应用资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>
<!--
<ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>
<ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
注意:如果我将两个ResourceDictionaries放在Appication.Resources MergedDictionary中(注释掉theme.xaml并取消注释其他两个词典),它们都会正确加载.但是,我们的资源定义方式,这可能意味着将加载相当多的资源,而对于动态加载,我希望能够定义模板.
我在一个silverlight应用程序中使用prism框架,在单独的XAP中有多个模块.
我在我的shell项目中定义了一个资源字典.在我的模块中,我可以很好地使用资源,但由于模块与shell分离,直到它们在运行时加载,设计器不会显示它们或识别它们.
有没有办法让模块在设计时知道我的资源而不在每个视图xaml中合并我的资源文件?
我的资源文件位于"常见"项目中.
我有以下要求:
我的WPF应用程序包含几个模块(程序集),其中一些与UI相关.
我想创建一个包含一组通用样式的单个程序集,用于某些控件(例如自定义默认按钮样式),它应该自动应用于所有其他与UI相关的程序集,只需包含那个程序集,而不必指定显式资源键.
我不为每种控件提供样式,因此没有自定义样式的样式应该保留默认的Aero主题(包括内容模板等).
我不希望写我自己的,扩展Button类,或者类似的东西.
我希望它在设计时也能在Visual Studio 中工作,无论是在最终应用程序中还是在其他与UI相关的模块中.
由于样式是在程序集内定义的,我显然不能在那里有App.xaml.因此,我假设我必须从Generic.xaml中包含它们.由于Generic.xaml仅在标准(Aero)主题中未定义样式时用作后备,因此WPF忽略我在Generic.xaml中的样式.
下一步可能是创建我自己的主题(以某种方式合并默认的Aero样式).但是,如何告诉VS在应用程序和模块中使用该主题,而不是例如Aero?我想我必须以声明方式执行此操作,因为我需要对自定义样式的设计时支持.