我正在开发一个C#MEF / Caliburn.Micro应用程序,它有一些样式,颜色等的资源字典。我试图给每个样式提供自己的文件,我是遇到基于其他资源字典中的样式的样式的问题。所有应用程序范围的资源都存在于Infastructure
项目中,这些资源已合并到App.xaml
。
这是我的解决方案布局:
Solution
->Shell
App.xaml
(other shell-related files)
->Infastructure
->ResourceDictionaries
->Brushes
DefaultColorTheme.xaml
->ControlStyles
ButtonStyle.xaml
DeleteButtonStyle.xaml
EditButtonStyle.xaml
ResourceLibrary.xaml
ResourceLibrary.xaml是ResourceDictionaries文件夹中所有资源字典的合并。
ResourceLibrary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes/DefaultColorTheme.xaml"/>
<ResourceDictionary Source="ControlStyles/ButtonStyle.xaml"/>
<ResourceDictionary Source="ControlStyles/DeleteButtonStyle.xaml"/>
<ResourceDictionary Source="ControlStyles/EditButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ResourceLibrary.xaml合并到App.xaml
的App.xaml
<Application...
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Infastructure;component/ResourceDictionaries/ResourceLibrary.xaml"/>
<ResourceDictionary>
<local:Bootstrapper x:Key="Bootstrapper"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
ButtonStyle.xaml包含以ButtonBase
为目标的主按钮样式,以及将主按钮样式应用于Button
和ToggleButton
的两种匿名样式
ButtonStyle.xaml
<Style x:Key="ButtonStyle" TargetType="{x:Type ButtonBase}">
...
</Style>
<Style BasedOn="{StaticResource ButtonStyle}" TargetType="{x:Type Button}"/>
<Style BasedOn="{StaticResource ButtonStyle}" TargetType="{x:Type ToggleButton}"/>
这很好用。 get中的所有Button
和ToggleButton
都应用了样式。
我还有两个其他按钮样式,它们在应用程序DeleteButtonStyle
和EditButtonStyle
中大量使用,需要基于ButtonStyle
。这是来自DeleteButtonStyle.xaml的代码片段。 EditButtonStyle类似。
<Style x:Key="DeleteButtonStyle" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource ButtonStyle}">
....
</Style>
一切都在设计时有效。这些按钮获得ButtonStyle
样式,明确标有...Style="{DynamicResource DeleteButtonStyle}"...
的按钮会将DeleteButtonStyle
应用于它们。但是,在运行时,我总是得到以下异常:
System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message='Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '4' and line position '35'.
Source=PresentationFramework
LineNumber=4
LinePosition=35
StackTrace:
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
...
InnerException:
HResult=-2146233088
Message=Cannot find resource named 'ButtonStyle'. Resource names are case sensitive.
Source=PresentationFramework
StackTrace:
at System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference)
at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
InnerException:
如果我将DeleteButtonStyle
和EditButtonStyle
移动到ButtonStyle
所在的同一文件中(ButtonStyle.xaml),就像这样
<Style x:Key="ButtonStyle" TargetType="{x:Type ButtonBase}">
...
</Style>
<Style BasedOn="{StaticResource ButtonStyle}" TargetType="{x:Type Button}"/>
<Style BasedOn="{StaticResource ButtonStyle}" TargetType="{x:Type ToggleButton}"/>
<Style x:Key="DeleteButtonStyle" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource ButtonStyle}">
....
</Style>
<Style x:Key="EditButtonStyle" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource ButtonStyle}">
....
</Style>
然后一切正常。
如何将依赖于其他样式的样式分隔到自己的资源字典中?
答案 0 :(得分:1)
在DeleteButtonStyle
和EditButtonStyle
<ResourceDictionary Source="ControlStyles/DeleteButtonStyle.xaml"/>
<ResourceDictionary Source="ControlStyles/EditButtonStyle.xaml"/>
您必须再次明确合并两个文件中的ResourceDictionary
以加载ButtonStyle
。
所以
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Infastructure;component/ResourceDictionaries/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>