从 MEF 加载的 XAP 访问嵌入式资源
简单的问题。
所以MEF不支持导入或导出松散文件(例如xml文件)等。
但是,它至少应该支持嵌入式资源,对吗?
我目前有一个动态加载 xap 的 silverlight 应用程序。这些动态加载的 xap 每个都有一个作为嵌入式资源附加的 xml 文件,可通过如下所示的实例方法访问...
public XDocument MenuStructure
{
get
{
return XDocument.Load("myFile.xml");
}
}
但是,此属性在导入后失败,并显示一条消息“在应用程序中找不到文件‘myFile.xml’” xap 包。”
我不确定问题是否在于我现在如何访问该文件,因为它的 BuildAction 设置为 EmbeddedResource 或不。
有什么想法吗?
谢谢
Simple question.
So MEF doesn't support importing or exporting loose files (such as xml files) etc.
However, it should at least support embedded resources right?
I currently have a silverlight application that loads xaps dynamically. These dynamically loaded xaps each have an xml file attached as an embedded resource accesible via an instance method that looks something like this...
public XDocument MenuStructure
{
get
{
return XDocument.Load("myFile.xml");
}
}
However, this property fails after import with a message saying "Cannot find file 'myFile.xml' in the application xap package."
I'm not sure whether the problem is how I'm accessing the file now that it's BuildAction is set to EmbeddedResource or not.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,根据 http://msdn.microsoft.com /en-us/library/ms596994(VS.95).aspx 我应该使用Application.GetResourceStream。现在一切都很好。
Ok, according to http://msdn.microsoft.com/en-us/library/ms596994(VS.95).aspx I was supposed to use Application.GetResourceStream. Everything works great now.
您是正确的,MEF 不支持从辅助下载的 XAP 加载资源。但是,您可以执行嵌入资源(嵌入到程序集中而不是 XAP 中),但访问它的方式不会从嵌入资源中提取文件。
对于 BuildAction EmbeddedResource,您需要从 Assembly.GetManifestResourceStream(...)(http://msdn.microsoft.com/en-us/library/xc4235zt.aspx)。
对于 BuildAction 资源,您需要构建适当的包 uri(请参阅 http://msdn.microsoft.com/en-us/library/aa970069(VS.85).aspx) 并传递给 Application.GetResourceStream (我实际上不是完全确定此方法是否适用于动态加载的 XAP)。
You are correct that MEF does not support loading resources from secondary downloaded XAP's. You can however do embedded resources (embedded in the assembly not the XAP) but the way you are accessing it will not pull the file from the embedded resources.
For BuildAction EmbeddedResource you will need to get the stream from the Assembly.GetManifestResourceStream(...)(http://msdn.microsoft.com/en-us/library/xc4235zt.aspx).
For BuildAction Resource you will need to build a proper pack uri (see Resource File Pack URIs - Referenced Assembly in http://msdn.microsoft.com/en-us/library/aa970069(VS.85).aspx) and pass to Application.GetResourceStream (I'm actually not entirely sure if this approach works for dynamically loaded XAPs or not).