Jan Keijzer’s Weblog

19/11/2010

Silverlight: Hoe refereer je een image, die in je XAP-file staat?

Gearchiveerd onder: C#, Silverlight — Jan Keijzer @ 17:34

Af en toe is het nodig om een resource uit je eigen XAP-file te refereren. Er zijn veel voorbeelden te vinden op internet hoe je dat met WPF doet. Maar die werken vaak niet omdat de Assembly.GetName() functie in Silverlight niet werkt (zie referentie hieronder). In Silverlight werkt de volgende methode het beste:

// Get the simple name of the application
string appName = Application.Current.GetType().Assembly.FullName.Split (',') [0];

// Get the string that points to the image file in the subfolder Images
string uriStr = "/" + appName + ";component/Images/" + imageFilename;

// Get the relative URI
Uri uri = new Uri(uriStr, UriKind.Relative);

// Create the ImageSource for the image
ImageSource imgSource = new BitmapImage(uri);

Let op de “/”, waarmee de uriString start.

Referenties:

Theme: Silver is the New Black. Blog op Wordpress.com.

Follow

Get every new post delivered to your Inbox.