MINI SUPPORTERS:

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.


Need Help?

If you have questions, need some assistance on this topic, or just want to chat - please drop by our friendly forums and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out. Plus, we have a large collection of smileys you can use

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly at kirupa[at]kirupa.com.

Cheers!

Kirupa Chinnathambi
about | facebook | twitter

.

1 | 2 | 3

SUPPORTERS:

cloud storage
cloud storage
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
X-Platform Application Development for Flash Free Flash Components Download - XML Templates, Players and Galleries.

two computer monitors

US Direct

Learn how to advertise on kirupa.com  
 
SHARE:



MINI SUPPORTERS: