合并的资源字典可以从App.xaml访问资源吗?

时间:2016-11-16 16:43:38

标签: xaml uwp windows-10-mobile

可以merged resource dictionariesApp.xaml访问资源吗?目标是将样式拆分以使其更具可读性。

这就是我正在寻找的,但不会以这种方式运作:

UWP项目中的

App.xaml

<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文件中复制所需的值,但是没有其他选项吗?合并字典可以包含另一个合并字典吗?或类似的东西?

2 个答案:

答案 0 :(得分: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>

SizesAndLayout:

<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中的设计器如果不是自上而下的格式,则不会呈现样式。

祝你好运!

答案 1 :(得分: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>