简体   繁体   English

棱镜中的动态资源

[英]dynamic resources in prism

I cannot get a button style to work in a Prism 4.0 app with four modules. 我无法在具有四个模块的Prism 4.0应用程序中使用按钮样式。 Here is the button element from the xaml view file in Module2: 这是Module2中xaml视图文件中的button元素:

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

The app builds and runs but the button color does not appear. 该应用会生成并运行,但按钮颜色不会出现。 I have defined the style in the Generic.xaml file in the Themes folder of the Shell Module. 我已经在Shell模块的Themes文件夹的Generic.xaml文件中定义了样式。 That is supposed to be where one can place styles to be shared between the modules. 那应该是可以放置样式在模块之间共享的地方。 In the Generic.xaml file I have: 在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>

I also have the necessary reference in the AssemblyInfo.cs file in the Properties folder of the Shell Project. 我在Shell项目的Properties文件夹中的AssemblyInfo.cs文件中也有必要的参考。 This should direct the Prism app to resolve all styles references from the Prism Generic.xaml file: 这应该指导Prism应用程序解析Prism Generic.xaml文件中的所有样式引用:

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

Those are still the original settings that were provided by the Prism WPF Unity template with which I started, provided by David Hill's blog [http://blogs.msdn.com/b/dphill/archive/2011/01/16/prism-4-0-template-pack-now-available.aspx] . 这些仍然是我开始使用的Prism WPF Unity模板所提供的原始设置,该模板由David Hill的博客提供[http://blogs.msdn.com/b/dphill/archive/2011/01/16/prism- 4-0-template-pack-now-available.aspx] The template came with some styles already in Generic.xaml but the bare template app only used those styles for controls in the Shell assembly, hence the parameters, "None" and "SourceAssembly" shown above. 该模板在Generic.xaml中已经带有一些样式,但是裸模板应用程序仅将这些样式用于Shell程序集中的控件,因此上面显示了参数“ None”和“ SourceAssembly”。

Since I am trying to define styles for use in modules other than the Shell module, I added the following ThemeInfo attribute to the AssemblyInfo.cs of Module1 and Module2L 由于我试图定义样式以在Shell模块以外的模块中使用,因此我将以下ThemeInfo属性添加到Module1和Module2L的AssemblyInfo.cs中

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

I Tried adding a ThemeDictionary extension to App.xaml like this in App.xaml & no result. 我试图在App.xaml中将ThemeDictionary扩展名添加到App.xaml中,但没有结果。

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

Also tried a pack url like this in App.xaml & got "Cannot locate resource" error. 还尝试了在App.xaml中这样的打包网址,并收到“找不到资源”错误。

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

Any thoughts or ideas as to what is missing? 关于缺少的东西有什么想法或想法吗? Thank you. 谢谢。

UPDATE: I got the answer from Marc Rodman at Microsoft through technical support. 更新:我通过技术支持从Microsoft的Marc Rodman得到了答案。 Two changes: 两项更改:

One was in Generic.xaml, changing the style definition to: 其中一个在Generic.xaml中,将样式定义更改为:

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

The other change was to App.xaml: 另一个更改是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM