Storing resources in XAP package - silverlight

Is it possible to store resx files and access them from the application package without changing any code?
EDIT: I don't want to embed them into the assembly. The resource files should be able to be modified without compiling the project so I tought storing them in XAP and replace when needed.
Thanks.

It is possible to include any file in a XAP by specifying that its build action to be Content.
However I'm not sure that is useful to you in the way you are hoping. Silverlight can only utalise a resx file in the intended manner if it is embedded in a dll.
Instead of using .resx files consider using ResourceDictionary .xaml files.

a XAP file is encrypted. you could load resources into isolated storage and access them in a similar way to a resource.

Related

Include data files in a Silverlight app

I have some XML files marked as "Content" that should be copied into the application's XAP file. How can I read these files from within Silverlight?
I know how to read files normally in .NET. So what I'm looking for is a way to find where Windows stuck the files and any realavent security issues.
This is not exactly how to read files from within the xap but if you just want to be able to load the files at runtime then this will work.
You can set the files to be embedded resources. You then get access to them using the following:
GetType().Assembly.GetManifestResourceStream(resourceName)
resourceName is the name of the file with a full namespace. E.g. if your assembly default namespace (set in project settings) is "foo" and your file is in a folder called "bar" then resourceName would be something like "foo.bar.MyFile.xml"
You should just be able to do an XDocument.Load an pass in the name of your file.

Reducing .XAP file size?

I created a customized version of the Silverlight Player. After building/compiling, the .XAP which gets pushed out, it ends up being over 700KB.
I'm not too sure why it's doing this because I'm not adding any new resources. All my changes have all been in the code.
Is there any way to reduce the file size?
You are probably including DLLs you did not intend to. The Copy Local option for DLL references you add is True by default.
A XAP file is just a ZIP file with a different extension so the first thing to do is unzip it and take a look at what is included.
You can use a better Zip program to recompress the XAP file, but that will only gain you another 10%-20% at best.
If it turns out you are really just concerned about initial load time vs. total load time you can load an initial (small) bootstrapper to update the display while loading subsequent modules dynamically (look at PRISM and MEF).
You can check the "Reduce XAP size by using application library caching" option.
By default any system dlls that your application uses will be included in the XAP file. By checking this option they are downloaded as separate zip files.
This won't reduce the total size of the files that your application downloads, but will mean that when you publish an update your users won't have to download all the system dlls again - they'll already have these in their local cache.
This option is primarily used in applications that are split across several XAP files to ensure that only one copy of the system dlls are downloaded for the whole application.

How to optimize the size of silverlight XAP file by removing common files?

I have bunch of common thrid party dlls that I use in multiple silver light projects. Each produce its own XAP files with same copy of the thrid party dlls. I would like to know, is it possibl to seperate the common thrid party dlls out and load only if the local cache dll is different from the common dll used by some other XAP that was previously loaded?
In other words, what I was thinking is, first time when I enter a new silverlight page, it would load the application XAp and the Common XAP file, from that silverlight app, I go to another silverlight page, which in turn loads another XAP but this time, it will not load the common dll XAP since it was already loaded by the first XAP load.
Thanks,
If the third-party DLL's are well done, they'll come with an .extmap.xml file that allows for precisely this scenario. But you need to turn it on by checking the "Reduce XAP size by using application library caching" option in your Silverlight project(s). See http://msdn.microsoft.com/en-us/library/dd833069(VS.95).aspx for more details about how this works.
I think a more optimal solution would be to try to minify your XAP/Zip files using something like ComponentOne XAPOptimizer. The problem with your approach is that you are introducing dependencies between projects that in no way could be managed if one or more projects change. This is a good resource for strategies to lower XAP file size.
I believe that you have a main XAP which makes the calls to load the others XAPs, right?
All you have to do is, in the properties of each DLL, change the atribute "Copy to Local" to FALSE, except on your main XAP.
When the user download the main XAP, the DLL will be available to all dependents XAP.

Accessing embedded resources from MEF loaded XAPs

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

Embed resources (png,jpg,mp3,wmv,etc) in silverlight xap file and reference from xaml

Basically I would like to be able to do something like this in my xaml:
<Image Source="Resources/logout.png" />
and have it find Resources/logout.png as a file in my xap file, without going back to the web server. I also want to be able to rebuild my xap file on the fly, so placing it as a resource in a dll is not acceptable.
Any ideas?
Add the images to the project, but set the Build Action to Content and Copy to Output Directory to Do not copy.
For a file in the root directory, I used this format.
<Image Source="/logout.png" />
I would suggest not doing this because it will make your initial XAP download take much longer.
To play back sounds embedded in the assembly DLL (this is different from embedding in XAP - in fact it is two levels nesting, since the DLL is in the XAP and gets loaded in memory) see http://www.dotnetspider.com/tutorials/Silverlight-Tutorial-326.aspx

Resources