views:

36

answers:

2

Hi all,

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

+1  A: 

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.

Hovito
+1  A: 

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

Wes Haggard