2012-01-22 72 views

回答

0

我這樣做,當一個特定的模塊加載,它運作良好。

在模塊的構造函數中,添加一個調用將加載資源的方法 - 這工作得很好,因爲做這種方式,我會被丟失資源的例外通知:

addResourceDictionaries(); 



protected void addResourceDictionaries() 
    { 
     LoadResource (new Uri("/NAME_OF_DLL;component/assets/name_and_path_of_xaml.xaml", UriKind.Relative)); 
    } 

private void LoadResource(Uri uri) 
    { 
     var info = System.Windows.Application.GetResourceStream(uri); 
     string xaml; 
     using (var reader = new System.IO.StreamReader(info.Stream)) 
     { 
      xaml = reader.ReadToEnd(); 
     } 

     ResourceDictionary result = System.Windows.Markup.XamlReader.Load(xaml) as ResourceDictionary; 

     if (result != null) 
     { 
      System.Windows.Application.Current.Resources.MergedDictionaries.Add(result); 
     } 
    } 
0

我使用以下命令:

出口我的ResourceDictionary與MEF(只是一個cs文件添加到您的ResourceDictionary)

[ExportResourceDictionary] 
public partial class MyResourcen : ResourceDictionary 
{ 
    public MyResourcen() 
    { 
     InitializeComponent(); 
    } 
} 

新的類文件添加到喲烏爾XAML

<ResourceDictionary x:Class="Test.Resourcen.MyResourcen"> 

導入你想要的資源,比如App.xaml中

[ImportMany("Resourcen", typeof(ResourceDictionary), AllowRecomposition = true)] 
private IEnumerable<ResourceDictionary> ImportResourcen { get; set; } 

    #region Implementation of IPartImportsSatisfiedNotification 

    public void OnImportsSatisfied() 
    { 
     foreach (var dic in ImportResourcen) 
     { 
      this.Resources.MergedDictionaries.Add(dic); 
     } 
    } 

    #endregion 

至少這裏有exportattribute

[MetadataAttribute] 
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
public class ExportResourceDictionaryAttribute : ExportAttribute 
{ 
    public ExportResourceDictionaryAttribute() : base("Resourcen", typeof(ResourceDictionary)) 
    { 

    } 
} 
+0

我忽略了Silverlight的標籤,不知道如果它的工作原理那裏 – blindmeis