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