2016-11-16 131 views
1

可以merged resource dictionariesApp.xaml访问资源?我们的目标是分割风格,使其更具可读性。合并资源字典可以从App.xaml访问资源吗?

这就是我要找的,但是以这种方式行不通:

的App.xaml在UWP项目

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 

    <!-- other custom styles, definitions, ThemeDictionaries, ... --> 
    <Color x:Key="Primary">#dfdfdf</Color> 
</Application.Resources> 

DefaultButtonStyle.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:AppName.UWP.Styles"> 

    <!-- some definitions --> 
    <Style TargetType="Button" x:Key="DefaultButtonStyle"> 
     <!-- my styles --> 
     <Setter Property="Background" Value="{StaticResource Primary}" /> 
    </Style> 
</ResourceDictionary> 

该应用程序崩溃

无法找到名称/键主

资源,我可以把一切都放在一个大style.xaml,或在每个XAML文件复制所需的值,但是是不是有其他的选择吗?合并的字典是否可以包含另一个合并的字典?或类似的东西?

回答

1

我已经使用分开的字典,并试图让他们在使用顺序。在我的应用程序有:

  • ColorsAndBrushes.xaml
  • SizesAndLayout.xaml
  • DefaultStyles.xaml
  • NamedStyles.xaml

凡ColorsAndBrushes看起来像:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <!-- Colors --> 
    <Color x:Key="Color_Banner">#FF333232</Color> 
    <!--overridden from themeresource--> 
    <Color x:Key="SystemChromeDisabledLowColor">#FFA8A49F</Color> 
    <Color x:Key="SystemAccentColor">#FF2877CF</Color> 
    <!-- /Colors --> 

    <!-- Brushes --> 
    <SolidColorBrush x:Key="Brush_Banner" Color="{StaticResource Color_Banner}" /> 
    <!-- /Brushes --> 
</ResourceDictionary> 

尺寸和布局:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <!-- Padding --> 
    <Thickness x:Key="Padding_Button">24,4</Thickness> 
    <Thickness x:Key="Padding_Dialog">10</Thickness> 
    <Thickness x:Key="Padding_Content">20</Thickness> 
    <!-- /Padding --> 

    <!-- Fonts --> 
    <FontFamily x:Key="Font_DefaultFamily">Segoe UI</FontFamily> 
    <FontWeight x:Key="Font_DefaultWeight">SemiLight</FontWeight> 
    <FontWeight x:Key="Font_NormalWeight">Normal</FontWeight> 
    <FontWeight x:Key="Font_BoldWeight">Semibold</FontWeight> 
    <x:Double x:Key="ContentControlFontSizeSmall">11</x:Double> 
    <x:Double x:Key="Font_NormalSize">20</x:Double> 
    <x:Double x:Key="Font_H1Size">36</x:Double> 
    <x:Double x:Key="Font_H2Size">28</x:Double> 
    <!-- /Fonts --> 
</ResourceDictionary> 

DefaultStyles(适用于所有类型的 - 其他2这些资源使用):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="ColorsAndBrushes.xaml" /> 
     <ResourceDictionary Source="SizesAndLayout.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 

    <Style TargetType="TextBlock"> 
     <Setter Property="FontFamily" Value="{StaticResource Font_DefaultFamily}" /> 
     <Setter Property="FontWeight" Value="{StaticResource Font_DefaultWeight}" /> 
     <Setter Property="FontSize" Value="{StaticResource Font_NormalSize}" /> 
     <Setter Property="TextWrapping" Value="WrapWholeWords" /> 
    </Style> 
</ResourceDictionary> 

和NamedStyles是默认的覆盖:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="ColorsAndBrushes.xaml" /> 
     <ResourceDictionary Source="SizesAndLayout.xaml" /> 
     <ResourceDictionary Source="DefaultStyles.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
    <Style x:Key="FontStyle_H1" TargetType="TextBlock" BasedOn="{StaticResource FontStyle_Default}"> 
     <Setter Property="FontSize" Value="{StaticResource Font_H1Size}" /> 
     <Setter Property="Foreground" Value="{StaticResource Brush_DarkBlue}" /> 
     <Setter Property="Margin" Value="{StaticResource Margin_TitleFont}" /> 
    </Style> 
</ResourceDictionary> 

最后,在App.xaml:

<Application 
    x:Class="MyApp.App.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App" 
    RequestedTheme="Light"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Styles/ColorsAndBrushes.xaml" /> 
       <ResourceDictionary Source="Styles/SizesAndLayout.xaml" /> 
       <ResourceDictionary Source="Styles/DefaultStyles.xaml" /> 
       <ResourceDictionary Source="Styles/NamedStyles.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

它适用于我,并通过使用较小范围的文件保持较小的XAML文件。但是,我会说有时候Visual Studio会给我一些额外的错误,抱怨它不能找出命名空间......但是只有在文件打开的时候。 我也经历过,在运行时,静态资源声明的顺序并不重要,有时,Visual Studio中的设计器不会呈现样式,如果它们不是自上而下的格式。

祝你好运!

+0

我学到的东西: - 使用正斜杠('/')。 - 在XAML中进行更改,重新编译,反转更改,重新编译,然后应用样式。 – testing

0

试试这个:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
     <!-- other custom styles, definitions, ThemeDictionaries, ... --> 
     <Color x:Key="Primary">#dfdfdf</Color> 
    </ResourceDictionary> 
</Application.Resources> 
+0

这似乎并不奏效。我得到多个错误,例如*“Key”属性只能用于“IDictionary”。*等内容中包含的元素。 – testing