棱镜中的动态资源

时间:2012-05-10 06:53:25

标签: styles prism generic.xaml

我无法在具有四个模块的Prism 4.0应用程序中使用按钮样式。这是Module2中xaml视图文件的按钮元素:

<Button Name="add" Width ="60" Style="{DynamicResource Red}"  Click="add_Click"> Add</Button>

应用程序构建并运行但不显示按钮颜色。我已在Shell模块的Themes文件夹中的Generic.xaml文件中定义了样式。这应该是可以在模块之间共享样式的地方。在Generic.xaml文件中,我有:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Shell.Controls" 
    xmlns:wc="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">

 <Style 
   x:Key="{ComponentResourceKey 
    TypeInTargetAssembly={x:Type wc:Button},
    ResourceId=Red}"
    TargetType="wc:Button">
    <Setter Property="Foreground" Value="Red" />
  </Style>
</ResourceDictionary>

我还在Shell项目的Properties文件夹中的AssemblyInfo.cs文件中有必要的引用。这应该指示Prism应用程序解析Prism Generic.xaml文件中的所有样式引用:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

这些仍然是我开始使用的Prism WPF Unity模板提供的原始设置,由David Hill的博客[http://blogs.msdn.com/b/dphill/archive/2011/01/16/prism-4-0-template-pack-now-available.aspx]提供。模板附带了Generic.xaml中的一些样式,但裸模板应用程序仅将这些样式用于Shell程序集中的控件,因此上面显示了参数“None”和“SourceAssembly”。

由于我试图定义在Shell模块以外的模块中使用的样式,我将以下ThemeInfo属性添加到Module1和Module2L的AssemblyInfo.cs

[`assembly: ThemeInfo(ResourceDictionaryLocation.ExternalAssembly, ResourceDictionaryLocation.ExternalAssembly)]`

我尝试在App.xaml&amp;中添加一个像App.xaml这样的ThemeDictionary扩展。没有结果。

    <Application.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="{ThemeDictionary MyApp}"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>

在App.xaml&amp;中也尝试了这样的包URL。得到“无法找到资源”错误。

    <Application.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MyApp;Shell/Themes/Generic.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>

关于缺少什么的任何想法或想法?谢谢。

1 个答案:

答案 0 :(得分:0)

更新:我通过技术支持得到了微软的Marc Rodman的回答。两个变化:

一个在Generic.xaml中,将样式定义更改为:

    <Style
x:Key="Red"
TargetType="Button">
    <Setter Property="Foreground" Value="Red" />
    </Style>

另一个变化是App.xaml:

  <Application.Resources>

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
     <ResourceDictionary
Source="pack://application:,,,/Shell;component/Themes/Generic.xaml"/>
     </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
    </Application.Resources>
相关问题