开发者

Accessing embedded resources from MEF loaded XAPs

开发者 https://www.devze.com 2023-01-04 00:09 出处:网络
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?

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 sil开发者_如何学编程verlight 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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消