这是我的方法-
在您的 App.xaml 中,您需要像这样声明 MergedDictionaries 元素..
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\Colors.xaml" />
<ResourceDictionary Source="Styles\Brushes.xaml" />
<ResourceDictionary Source="Styles\Typeography.xaml" />
<ResourceDictionary Source="Styles\ModuleAStyles.xaml />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
如您所见,我倾向于在这里为颜色(如果拼写正确,则为颜色)、画笔、排版设置几个单独的文件,然后我通常为每个模块使用一个额外的文件.如果它是一个大型应用程序,请使用多个应用程序,否则您最终会在一个文件中包含太多内容,并且很快就会变得难以维护。您只需要在这里使用您的最佳判断,看看什么最适合您。
在我在合并字典中引用的 Typeography.xaml 中,我声明了一个名为 ApplicationTitle 的样式,如下所示...
<!--Application Title-->
<Style TargetType="{x:Type TextBlock}"
x:Key="ApplicationTitle">
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="FontFamily"
Value="Georgia" />
<Setter Property="Foreground"
Value="{StaticResource ResourceKey=FlatGradientLightest}" />
<Setter Property="FontSize"
Value="24" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="5"
Color="#333"
Opacity=".3" />
</Setter.Value>
</Setter>
</Style>
您会注意到,在这种风格中,我再次使用StaticResource 标记扩展名引用另一个名为“FlatGradientLightest”的资源。此资源存在于 Brushes.xaml 文件中...
<!--Flat Diagonal Gradient Lightest-->
<LinearGradientBrush x:Key="FlatDiagonalGradientLightest"
StartPoint="0,0"
EndPoint="1,1">
<GradientStop Color="{StaticResource ResourceKey=Light}"
Offset="0" />
<GradientStop Color="{StaticResource ResourceKey=Lightest}"
Offset="1" />
</LinearGradientBrush>
这再次引用了颜色“Light”和“Lightest”。这些存在于 Colors.xaml 文件中...
<Color x:Key="Light"
A="255"
R="190"
G="190"
B="190" />
<Color x:Key="Lightest"
A="255"
R="250"
G="250"
B="250" />
这里重要的是我在 App.xaml 中指定资源字典的顺序。如果我将 Typeography.xaml 移到列表顶部,则会导致运行时,因为在加载此样式时,它的依赖项将不存在。
由于 WPF/SL 向上查找以解析资源(如 Muad'Dib 指出的那样),因此您可以在模块中使用这些资源,就好像它们是在本地声明的一样。所以在我的一个模块中,我有一个像这样声明的TextBlock...
<TextBlock Text="Menu Module Loaded" Style="{StaticResource ResourceKey=ApplicationTitle}" />
TextBlock 现在使用 Typeography.xaml 中的“ApplicationTitle”样式,它从 Brushes.xaml 中的“FlatGradientLightest”LinearGradientBrush 加载它的 Foreground 画笔,然后从 Color 资源加载两种颜色在 Colours.xaml 文件中。
有点酷吧?
希望对您有所帮助。