合并的资源字典可以从 App.xaml 访问资源吗? [英] Can merged resource dictionaries access resources from App.xaml?

查看:21
本文介绍了合并的资源字典可以从 App.xaml 访问资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以 合并的资源字典App.xaml 访问资源?目标是拆分样式以使其更具可读性.

Can merged resource dictionaries access resources from App.xaml? The goal is to split the style to have it more readable.

这就是我要找的,但不是这样的:

This is what I'm looking for, but doesn't work in that way:

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>

应用程序崩溃

无法找到具有名称/主键的资源

Cannot find a Resource with the Name/Key Primary

我可以把所有东西都放在一个大的 style.xaml 中,或者复制每个 xaml 文件中需要的值,但没有其他选择吗?合并的字典可以包含另一个合并的字典吗?或者类似的东西?

I could put everything in one big style.xaml, or copy the needed values in each xaml file, but aren't there other options? Could a merged dictionary include another merged dictionary? Or something like that?

推荐答案

我使用了分开的字典,并试图让它们按使用顺序排列.在我的应用程序中,我有:

I have used separated dictionaries and have tried to keep them in order of usage. In my application I have:

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

ColorsAndBrushes 看起来像:

Where ColorsAndBrushes looks something like:

<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 种的资源):

DefaultStyles (apply to all of type - these use resources from other 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 是默认值的覆盖:

and NamedStyles are overrides of the default:

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

And finally, in the 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 中的设计器将不会呈现样式.

It works for me and keeps the XAML files smaller by using the smaller scoped files. However, I will say there are times that Visual Studio will give me a bunch of squigglies complaining that it can't figure out the namespace... but only when the file is open. I have also experienced that, while at runtime, the order of the declaration of static resources does not matter, at times, the designer within Visual Studio will not render the styles if they aren't in a top-down format.

祝你好运!

这篇关于合并的资源字典可以从 App.xaml 访问资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆