wpf 如何使用 ResourceDictionary 中定义的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22114260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:04:28  来源:igfitidea点击:

how to use styles defined in ResourceDictionary

wpfxamlwindows-8.1resourcedictionary

提问by Jakub Kuszneruk

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

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

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

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

I tried this:

我试过这个:

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

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

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

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

回答by Bizhan

add the following tag to your App.Xaml:

将以下标记添加到您的 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:

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

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

if in the same project by its relative path:

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

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

or defined inline:

或内联定义:

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