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 :
-
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.
Hi,
at least reference the book where you took the examples from. :-p
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.