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

标签 wpf xaml windows-8.1 resourcedictionary

我有几种常见的样式,我想在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}"/>

最佳答案

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

关于wpf - 如何使用ResourceDictionary中定义的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22114260/

相关文章:

c# - WPF- TextBlock 中的 "LineSpacing"

c# - WPF 理解 Selector.IsSynchronizedWithCurrentItem

c# - 如何在 metro 风格应用程序 xaml 2012 中制作打开文件对话框

javascript - 调用 window.open() 在 IE 11 + Windows 8.1 Preview 上返回 null

c# - 为什么我的 RibbonWindow 主题不正确?

wpf - mvvm 更新计算字段

c# - CollectionViewSource 使用问题

c# - 在 TreeView 中使用 HierarchicalDataTemplates

for-loop - Gnuplot 5.0补丁lvl1在WIN 8.1上崩溃

python - 在 Windows 8、python 3.3 中创建虚拟环境时遇到问题