OKAY another edit: Even though the above method DOES put all your images/sound/content into a different project, and compiles it into a .dll, there is still a problem. Inside my silverlight app, you must "Add Reference" to
this .dll to actually use it. And well............this .dll ALSO gets included into your XAP file, making your XAP exactly the same size as it was before......in my case huge.
So here is another extended, crazy solution THAT IS CONFIRMED TO WORK. I used the sample code at this website:
Now in my visual web solution file, I have a website project, I have my silverlight project, a library project for my assets, and now a NEW library project as an "Interface". Think of it as a middle-man between my assets and my silverlight
So in this new library project, I place one new file where I create an "interface" class, just like the "Interfaces" project mentioned in the link a couple lines above. Here is my code:
public interface ITexture
BitmapImage GetBitmap(string _filePath);
Wow isn't that amazing. NOW I go back to my asset library project. Here I place my "ResourceHelper" file (as mentioned in the previous post) and
create a new file that will inherit my new interface:
public class MyTexture: ITexture
public BitmapImage GetBitmap(string _filePath)
BitmapImage tempBitmap = ResourceHelper.GetBitmap(_filePath, "AssetLibrary");
In this example, the filepath is relative to the asset library project (meaning, your images must be placed in this project to be accessed). Please look toward my previous
for information on the ResourceHelper.
Remember you need to "Add a Reference" to your "Interface" library project. That's the only way you'll get access to "ITexture"
(or whatever you decide to call it)
So now, we need to open the assembly (.dll of the AssetProject) inside your SILVERLIGHT PROJECT to actually use it.
First, "Add a reference" to your interface project.
Secondly, go to the "Page.xaml.cs" file. Here is some code:
public partial class Page : UserControl
this.Loaded += new RoutedEventHandler(Page_Loaded);
private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
WebClient client = new WebClient();
client.OpenReadCompleted += OnAssemblyOpened;
client.OpenReadAsync(new Uri("AssetLibrary.dll", UriKind.Relative));
void OnAssemblyOpened(object sender, OpenReadCompletedEventArgs e)
AssemblyPart assemblyPart = new AssemblyPart();
System.Reflection.Assembly assembly = assemblyPart.Load(e.Result);
// The string I'm passing in here has this format: namespace.className
// (namespace of your asset library project, name of your class that implements the interface)
ITexture tempTexture = assembly.CreateInstance("AssetLibrary.MyTexture") as ITexture;
BitmapImage thisBitmap = tempTexture.GetBitmap("Textures/MyAwesomeImage.png");
Woot, now you can load images WITHOUT having to include them in your XAP file. BUT PLEASE NOTE!!!!
YOU MUST MANUALLY COPY your "AssetProject.dll" (whatever you name your asset project file) and PASTE IT IN THE CLIENTBIN FOLDER
OF YOUR WEBSITE PROJECT. Your XAP file and this asset .dll must both be in the clientbin folder of your website project to actually work.
I tried to create this method a couple times and it took me days to get it right. So I hope this code can help someone too!