我有一个silverlight应用程序和generic.xaml文件。 在通用文件中,我想合并由MEF从其他DLL导入的字典。Silverlight MEF - 导入资源并放入generic.xmal
我该怎么做? (一个例子会很好)
我有一个silverlight应用程序和generic.xaml文件。 在通用文件中,我想合并由MEF从其他DLL导入的字典。Silverlight MEF - 导入资源并放入generic.xmal
我该怎么做? (一个例子会很好)
我这样做,当一个特定的模块加载,它运作良好。
在模块的构造函数中,添加一个调用将加载资源的方法 - 这工作得很好,因为做这种方式,我会被丢失资源的例外通知:
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);
}
}
我使用以下命令:
出口我的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))
{
}
}
我忽略了Silverlight的标签,不知道如果它的工作原理那里 – blindmeis