我正在开发一个 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 文件夹中所有资源字典的合并。
资源库.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
应用程序.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
按钮样式.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
s 和ToggleButton
s 都应用了该样式。
我还有另外两种在应用程序中大量使用的按钮样式,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>
然后一切正常。
如何将依赖于其他样式的样式分离到自己的资源字典中?