如何使用ResourceDictionary中定义的样式

问题描述 投票:9回答:1

我有几种共同的风格,我想在Windows 8.1应用程序的多个页面中共享它们。

我知道我可以用merge dictionaries option实现,但是我不知道如何使用字典中定义的样式。

我尝试过:

<Page.Resources>
<ResourceDictionary x:Key="lol">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/Generic.xaml" />
        <ResourceDictionary>
            <Style x:Key="TextViewAllStyle" TargetType="TextBlock">
            </Style>
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="TextViewAllStyle2" TargetType="TextBlock">
    </Style>
</ResourceDictionary>
<Style x:Key="TextViewAllStyle3" TargetType="TextBlock">
</Style>
</Page.Resources>

但是我的Visual Studio仅看到第三个...

<TextBlock Style="{StaticResource ResourceKey=TextViewAllStyle3}"/>
wpf xaml windows-8.1 resourcedictionary
1个回答
20
投票

将以下标签添加到您的App.Xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            -- reference your dictionaries here --
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后是-- reference your dictionaries here --部分:每个字典都可以通过其完整路径添加:

<ResourceDictionary Source="pack://application:,,,/MySolution.MyProject;component/Theme/Generic.xaml"/>

如果在同一项目中,通过其相对路径:

<ResourceDictionary Source="Themes\Generic.xaml"/>

或定义的内联:

<Style x:Key="TextViewAllStyle" TargetType="TextBlock">
</Style>
© www.soinside.com 2019 - 2024. All rights reserved.