Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

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.


I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

.

1 | 2 | 3


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com