Home > Resources, WPF > Resources in WPF – I (Binary Resources)

Resources in WPF – I (Binary Resources)

There are two types of resources in WPF :

1) Binary Resources
2) Logical Resources

In this post, I have described the details of the Binary resources.

Binary Resources : Binary resources could be logo/image files, AV files etc. Resource files which are added to project, can have the “Build Action” defined on it from the file properties windows.

Out of the various build actions defined by WPF only following two are important for binary resources :

image

  • Resource : Embeds resource into the assembly (or culture specific satellite assembly)
  • Content : this leaves resource as loose file and upon compilation this resource information is not embedded to assembly. Instead, it adds custom attribute to the assembly (AssemblyAssociatedContentFile) which records the existence and relative location of file.It is also possible to access the resource file without adding into the project however with this approach management of resource file becomes bit difficult. However, this approach is useful is resource file is generated dynamically using some runtime information. In such a case, resource file will not be available at compile time so can not be added to project.

What is the difference between the “Resource” and “Embedded Resource” build actions?
“Resource” and “Content” build actions are to access the WPF resources using the Uris. However “Embedded Resource” is for prior technologies. However both options embeds the resource in assembly but “Resource” option to be used for WPF.

URIs for accessing the binary resources

URI

Resource

“Logo.jpg”
  • embedded in current assembly (using “Resource” as build action)
  • loose resource however physical file is placed along with xaml file or assembly (if selected build action is “Content”)
“A/B/Logo.jpg” same as above but resource file is located in subfolder “A/B”
“D:\Resource\Logo.jpg” resource path hard coded
http://pinvoke.net/logo.jpg loose resource hosted at pinvoke.net website.
ResourceDll;component/Logo.jpg this URI represents the resource embedded in assembly ResourceDLL.dll or ResourceDLL.exe
ResourceDll;componen/tA/B/Logo.jpg same as above except resource is located in subfolder
pack://siteOfOrigin:,,,/logo.jpg resource located at site of origin
pack://siteOfOrigin:,,,/A/B/logo.jpg resource located at site of origin in sub folder

Site of Origin :

Site of origin at the runtime gets resolved in different ways depending upon the way in which the application has been deployed.

  • For a full-trust application installed with Windows Installer, the site of origin is the application’s root folder.
  • For a full-trust ClickOnce application, the site of origin is the URL or Universal Naming Convention (UNC) path from which the application was deployed.
  • For a partial-trust XAML Browser Application (XBAP) or ClickOnce application, the site of origin is the URL or UNC path that hosts the application.
  • For loose XAML pages viewed in Internet Explorer, there is no site of origin. Attempting to use it throws an exception.

Accessing resources from procedural code :

While accessing the resource from procedural code in WPF, triple comma syntax is used however above table for URIs remains still applicable except the last two rows with site of origin.
e.g. to read the embedded resource with “Resource” action “pack://application:,,,/logo.jpg’” can be used.

Following code shows how to read the resource from procedural code :

Image image = new Image();
image.Source = new BitmapImage(new Uri(“pack://application:,,,/logo.jpg”))

There is one more aspect to binary resource and that is localization but I’m not covering it as of now.

About these ads
  1. anonymous
    September 11, 2009 at 10:56 pm | #1

    Hi,

    at least reference the book where you took the examples from. :-p

    • September 11, 2009 at 11:17 pm | #2

      I have mentioned in my other post the book which I refer to for my WPF learning. Most of the time I read “WPF Unleashed” from Adam Nathan. So after reading it, I try to put my understanding in my words. It may or may not have the same kind of example from the book. Thanks for pointing out the matter and next time will try to be careful about it.

      But at the same time, I would have appreciated if you had commented with your true identity.

  2. December 25, 2009 at 11:37 am | #3

    Merry Christmas!
    I’m just confused with the differences between ‘Resource’ and ‘Embedded Resource’. The only thing I know now, is that ‘Resource’ is WPF-Only build action. However, what is the differences under the hood (fileformat or compiler behavior)?

    • February 20, 2010 at 4:11 pm | #4

      Dylan,

      I have taken following explaination from Adam Nathan book.

      —–

      “The Resource build action is confusingly similar to the EmbeddedResource build action (Embedded Resource in Visual Studio’s property grid). Both embed a binary resource inside an assembly, but the latter should be avoided in WPF projects. Whereas Resource was added specifically for WPF, EmbeddedResource predates WPF (and is the way binary resources are embedded in a Windows Forms project).
      WPF’s APIs that reference resources via uniform resource identifiers (described in the next section) are designed for resources that use a build action of Content or Resource only. This also means that resources embedded with the Content or Resource build action can be referenced easily from XAML, but resources embedded with the EmbeddedResource build action cannot be (unless you write some custom code).”

      —–

      Hope above explaination helps. The difference is how easily how you can access the resource information. For WPF, always use either “Resource” or “Content”.

  3. February 16, 2010 at 4:32 pm | #5

    @Dylan:
    As i know..an embedded resource means anything which is embedded into the project files. Suppose you want some image to show in the window whenever the application runs. Either you place it in the project itself(or embed it) or you can give an appropriate path. but in the later case it won’t be embedded since it is local to that system. But in both cases, the image can be thought of as a resource.
    I hope i have explained..!

  4. October 4, 2012 at 11:12 pm | #6

    I seriously am grateful for all the hard effort that you have done keeping this place available for everyone.
    I truly hope this sticks around for a good long while.

  5. January 17, 2013 at 6:43 am | #7

    My programmer is trying to convince me to move to .net
    from PHP. I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on
    numerous websites for about a year and am concerned about switching to
    another platform. I have heard good things about blogengine.
    net. Is there a way I can transfer all my wordpress posts into it?
    Any help would be greatly appreciated!

    • January 18, 2013 at 10:39 pm | #8

      Andreas,
      I hv very limited knowledge in the area for which you have asked the question. Regret to tell I’m not the right person to reply to your question.

  6. July 4, 2013 at 5:50 am | #9

    Thanks a lot for that great explanation! It helped me a lot to understand WPF and it’s resource handling.

  1. May 24, 2012 at 1:17 pm | #1
  2. August 23, 2013 at 9:14 pm | #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: