[英]Silverlight MEF - importing resources and putting into generic.xmal
I have a silverlight application with generic.xaml file. 我有一个带有generic.xaml文件的silverlight应用程序。 In the generic file I would like to merge dictionaries that are imported by MEF from other DLLs.
在通用文件中,我想合并由MEF从其他DLL导入的字典。
How can I do that ? 我怎样才能做到这一点 ? (an example would be nice)
(一个例子会很好)
I do this when one specific module loads, and it works well. 我在加载一个特定模块时执行此操作,并且效果很好。
In the constructor of the module, add a call to the method that will load the resources - this works well because by doing it this way, I will be informed by exceptions of missing resources: 在模块的构造函数中,添加一个对将加载资源的方法的调用-这样做很好,因为通过这种方式,我将收到缺少资源的异常通知:
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);
}
}
i use the following: 我使用以下内容:
export my resourcedictionary with mef (simply add a .cs file to your Resourcedictionary) 使用mef导出我的resourcedictionary(只需将.cs文件添加到Resourcedictionary)
[ExportResourceDictionary]
public partial class MyResourcen : ResourceDictionary
{
public MyResourcen()
{
InitializeComponent();
}
}
add the new class file to your xaml 将新的类文件添加到您的xaml
<ResourceDictionary x:Class="Test.Resourcen.MyResourcen">
import the resources where you want, eg app.xaml 导入所需的资源,例如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
at least here are the exportattribute 至少这里是导出属性
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportResourceDictionaryAttribute : ExportAttribute
{
public ExportResourceDictionaryAttribute() : base("Resourcen", typeof(ResourceDictionary))
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.