Using Resources for External Content - Page 3
       by kirupa  |  12 January 2007

In the previous pages we discussed how to use resources by accessing them from the manifest information stored in the assembly. There are newer methods that apply to WPF-specific situations, so let's take a look at them in this page.

Resources in WPF Applications
With the .NET Framework 3.0, you can create applications that use Windows Presentation Foundation (WPF). One of the changes introduced with .NET 3.0 is a different way of dealing with resources. Most of the steps from the previous sections still apply, and the basic idea remains the same. There are some details that are different, so let's take a look at them.

Importing External Files and Build Type
When importing external files into your WPF projects, your files' Build Type is automatically set to a new value that was not available in WinForms projects: Resource

[ notice that we have new Build Action types available ]

The differences between Resource and Embedded Resource are minor, but Resource was designed with WPF in mind. As I will explain next, the way you access a Resource from an assembly is different in the WPF world.

Referring to Resources in a WPF Application
If you want to assign a resource to a WPF-specific control, you will use a  new URI-based approach for specifying how your resource will be loaded. For example, as before, let's say I have a button called btnSubmit, and I want to set it's content to the blue.png image from earlier.

The code for displaying blue.png would be:

Image imageContent = new Image();
imageContent.Source = new BitmapImage(new Uri("pack://application:,,,
/blue.png"
));
btnSubmit.Content = imageContent;

The first thing to notice is that the earlier approach of accessing the manifest from the currently active assembly is no longer used. Instead, the bizarre syntax pack://application:,,,/blue.png is used to access our resource.

The bizarre syntax is part of the Pack Uniform Resource Identifier (URI) used in the XML Paper Specification (XPS), but I won't be devoting time in this tutorial to discuss the internals of why the syntax is the way it is. The reason is, for almost all of your applications, you will simply use the same URI string with the only change being the filename/extension. You do not even have to worry about specifying your application's namespace like you did earlier.

Mixing Embedding Approaches
The above method works only with content that supports URI-based resource handling such as all WPF-specific content. Because you may run into situations where you cannot use URIs, you can always use the traditional method for embedding resources.

The following code provides an example where you use both URIs as well as as the resource path:

Image imageContent = new Image();
imageContent.Source = new BitmapImage(new Uri
("pack://application:,,,/blue.png"));
btnSubmit.Content = imageContent;
 
TextReader tr = new StreamReader(Assembly.GetExecutingAssembly()
.GetManifestResourceStream("IncrementalSearch.words.txt"));
txtMain.Content = tr.ReadLine();

When attempting to use the assembly path approach, just remember to set the Build Type for those files to Embedded Resource.


Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

.

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.