[英]Silverlight MEF - importing resources and putting into generic.xmal
我有一個帶有generic.xaml文件的silverlight應用程序。 在通用文件中,我想合並由MEF從其他DLL導入的字典。
我怎樣才能做到這一點 ? (一個例子會很好)
我在加載一個特定模塊時執行此操作,並且效果很好。
在模塊的構造函數中,添加一個對將加載資源的方法的調用-這樣做很好,因為通過這種方式,我將收到缺少資源的異常通知:
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);
}
}
我使用以下內容:
使用mef導出我的resourcedictionary(只需將.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
至少這里是導出屬性
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportResourceDictionaryAttribute : ExportAttribute
{
public ExportResourceDictionaryAttribute() : base("Resourcen", typeof(ResourceDictionary))
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.