2012-05-10 19 views

回答

1

喜歡的東西:

ResourceDictionary resources = new ResourceDictionary(); 
resources.Source = new Uri("/MyModule;component/MyModule.xaml", 
    UriKind.RelativeOrAbsolute); 
Application.Current.Resources.MergedDictionaries.Add(resources); 

可能是你在找什麼。我們在Prism模塊中使用這樣的代碼。

3

從SketchFlow的項目,我對工作的全A段展示瞭如何在XAML合併資源字典:

<Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Microsoft.Expression.Prototyping.SketchControls;component/ScrollViewerStyles.xaml"/> 
      <ResourceDictionary Source="/[ProjectABC];component/[fileXYZ].xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

這說明合併兩個額外的資源字典到另一個資源字典。

(請注意,如果你有,因爲它們會互相覆蓋多個地方定義默認樣式的順序可以成爲重要的)

19

在Dictionary2.xaml定義MergedDictionaries(開放的ResourceDictionary標籤之後):

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/Path/to/Dictionary1.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

有一個問題:每次合併字典時,您都會有效地創建合併字典的副本。它是遞歸的 - 如果您有Dict3.xaml和Dict4.xaml都加載Dictionary2.xaml,您將創建三個Dictionary1.xaml實例

解決方案是SharedResourceDictionary。本教程中的實現應視爲一個起點,可能需要一定程度的調整 - 具體取決於使用場景。谷歌「wpf SharedResourceDictionary」的一些陷阱和解決方案。

從答覆到this questionXAMeLi

相關問題