2014-03-01 24 views
4

我有几种常见样式,我想在我的Windows 8.1应用程序的多个页面中共享它们。如何使用ResourceDictionary中定义的样式

我知道我可以用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}"/> 

回答

11

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

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

每个字典都可以通过完整路径添加:

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

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

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

或定义在线:

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

它给我一个错误的资源属性仅可一次设定。 – C4u

+0

您可能已在资源标签中定义了多个词典或其他资源。如果你将它们移动到MergedDictionaries,它应该没问题。请参阅此链接http://stackoverflow.com/a/3425956/366064 – Bijan

+0

感谢您的回复。我发现我的问题。远离形成定义的字典,我仍然努力在我的app.xaml内的一些自定义样式。我只需要在Merde-block中用''将它们包围起来。一切安好 :)。 – C4u

相关问题