资源字典中的样式仅在设计时有效

本文关键字:有效 字典 样式 资源 | 更新日期: 2023-09-27 17:57:15

我有一些样式用于在单独的文件/资源字典中定义的常见控件。该文件在 App.xaml 中引用,并在启动时加载。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <workbench:AppBootstrapper x:Key="bootstrapper" />
            </ResourceDictionary>
            <ResourceDictionary Source="Resources/Layout.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

资源/布局.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib">
    <Style TargetType="Label" x:Key="Heading1">
        <Setter Property="FontSize" Value="{StaticResource Heading1FontSize}" />
    </Style>
</ResourceDictionary>

在设计时,它可以完美运行。但是,当应用程序运行时,不会应用任何样式。我在这里可能缺少什么?

资源字典中的样式仅在设计时有效

您如何为所有label设置样式?您希望样式应用于所有label还是仅应用于特定的label控件?

如果要将其应用于所有类型的Label控件,则必须删除Style标记中的x:Key属性。

如果希望它特定于每个Label控件,则必须定义每个Label ControlStyle 属性,并将其指向使用 key

更多信息可以在这里找到

更新

尝试将Source更改为类似于此

pack://application:,,,/<AssemblyName>;component/Resources/Layout.xaml