合并的资源字典能否从App.xaml访问资源?

合并的资源字典能否从App.xaml访问资源?,xaml,uwp,windows-10-mobile,Xaml,Uwp,Windows 10 Mobile,是否可以从App.xaml访问资源?目标是拆分样式以使其更具可读性 这就是我要找的,但不是这样的: UWP项目中的App.xaml <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles\DefaultButtonStyle.

是否可以从
App.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>

#dfdfdf
DefaultButtonStyle.xaml


该应用程序因以下原因崩溃:

找不到名为/Key Primary的资源

我可以将所有内容放在一个大的style.xaml中,或者在每个xaml文件中复制所需的值,但是没有其他选项吗?合并词典是否可以包含另一个合并词典?或者类似的东西?

试试这个:

<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>

#dfdfdf

我使用过分开的字典,并试图按使用顺序保存它们。在我的申请中,我有:

  • ColorsAndBrushes.xaml
  • SizesAndLayout.xaml
  • DefaultStyles.xaml
  • NamedStyles.xaml
其中ColorSandwrush看起来像:

<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>

#FF333232
#FFA8A49F
#FF2877CF
大小和布局:

<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>

24,4
10
20
微软雅黑效果
半光灯
正常的
半黑体
11
20
36
28
DefaultStyles(适用于所有类型-这些类型使用其他2个类型的资源):


和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文件保持较小。然而,我要说的是,有时候VisualStudio会给我一堆歪歪扭扭的东西,抱怨它无法找出名称空间。。。但仅当文件打开时。 我还经历过,虽然在运行时,静态资源声明的顺序并不重要,但有时,如果样式不是自顶向下的格式,VisualStudio中的设计器将不会呈现样式


祝你好运

这似乎不起作用。我得到了多个错误,比如“Key”属性只能用于“IDictionary”中包含的元素。等等。我学到的东西:-使用正斜杠(
/
)。-在XAML中进行更改、重新编译、反转更改、重新编译,然后应用样式。
<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>
<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>