如何使用 ResourceDictionary 中定义的样式 [英] how to use styles defined in ResourceDictionary

查看:64
本文介绍了如何使用 ResourceDictionary 中定义的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个常用样式,我想在我的 Windows 8.1 应用程序的几个页面中共享它们.

I have several common styles and I want to share them in several pages of my Windows 8.1 application.

我知道我可以使用合并词典选项来实现,但是我不知道如何使用字典中定义的样式.

I know that I can achieve with merge dictionaries option, but I have no idea how to use styles defined in dictionary.

我试过了:

<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 只看到第三个...

But my Visual Studio sees only the third one...

<TextBlock Style="{StaticResource ResourceKey=TextViewAllStyle3}"/>

推荐答案

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

add the following tag to your App.Xaml:

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

然后对于 -- 在此处引用您的词典 -- 部分:每个词典都可以通过其完整路径添加:

Then for -- reference your dictionaries here -- part: each dictionary can be added by its full path:

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

如果在同一个项目中的相对路径:

if in the same project by its relative path:

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

或内联定义:

<Style x:Key="TextViewAllStyle" TargetType="TextBlock">
</Style>

这篇关于如何使用 ResourceDictionary 中定义的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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