繁体   English   中英

棱镜中的动态资源

[英]dynamic resources in prism

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

<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中将ThemeDictionary扩展名添加到App.xaml中,但没有结果。

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

还尝试了在App.xaml中这样的打包网址,并收到“找不到资源”错误。

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

关于缺少的东西有什么想法或想法吗? 谢谢。

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

暂无
暂无

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

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