簡體   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