【问题标题】:dynamic resources in prism棱镜中的动态资源
【发布时间】:2012-05-18 17:18:48
【问题描述】:

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

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

应用程序构建并运行,但按钮颜色未出现。我在 Shell Module 的 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 模块以外的模块中使用的样式,因此我在 Module1 和 Module2L 的 AssemblyInfo.cs 中添加了以下 ThemeInfo 属性

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

我尝试在 App.xaml 中像这样向 App.xaml 添加 ThemeDictionary 扩展,但没有结果。

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

还在 App.xaml 中尝试了这样的包 url 并得到“无法找到资源”错误。

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

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

【问题讨论】:

    标签: styles prism generic.xaml


    【解决方案1】:

    更新:我通过技术支持从 Microsoft 的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2021-11-06
      • 2017-03-26
      相关资源
      最近更新 更多