Thursday 26 January 2012

Basic WPF - Binary Resources

Resources In WPF

The purpose of this post (and subsequent) is a write up of notes made for the exam I am (re)sitting in a few weeks time... Windows Application Development in .Net 4.0. I am trying to use the process of writing these posts as a way of ensuring that have the subjects straight in my head.

The outline for the next few days study is the basics of the WPF control structure. This will include the following:

  • Resources;
  • Styles;
  • Templating.

All of which enable the WPF control structure which can be used to produce extremely flexible user interfaces.


Resource Types & Resource Handling

There are two types of resources used in WPF:
  • Binary;
  • Logical.


Binary resources are what developers would traditionally consider to be resources... images, files, etc. Chunks of binary that are required by the application at various times. 

Logical resources are chunks of XAML that define objects that can be reused around the application.

In WPF there is one system of handling/referencing resources regardless of whether they are binary or logical which I will describe after I have touched on the following... 

Binary Resources & Pack URI Syntax

Binary resource types...

The actual binary resource can be anything really, image, sound file, video, document, etc. However, as far as the WPF resource system is concerned there are two types of binary resource:
  • Embedded/Compiled Resource files;
  • Loose/Uncompiled Content files.
From MSDN:

Microsoft Windows applications often depend on files that contain non-executable data, such as Extensible Application Markup Language (XAML), images, video, and audio. Windows Presentation Foundation (WPF) offers special support for configuring, identifying, and using these types of data files, which are called application data files. This support revolves around a specific set of application data file types, including:

Resource Files: Data files that are compiled into either an executable or library WPF assembly.

Content Files: Standalone data files that have an explicit association with an executable WPF assembly.

Site of Origin Files: Standalone data files that have no association with an executable WPF assembly.
One important distinction to make between these three types of files is that resource files and content files are known at build time; an assembly has explicit knowledge of them. For site of origin files, however, an assembly may have no knowledge of them at all, or implicit knowledge through a pack uniform resource identifier (URI) reference; the case of the latter, there is no guarantee that the referenced site of origin file actually exists.
The choice between Resource and Content...

The choice between Resource and Content would be a case of how often a resource file is likely to change. If infrequently, then it should be included as a compiled resource. If more frequently, then it should be included as a loose content file which would allow it to be updated without the need to recompile the assembly.

On the flip side, the only way to absolutely guarantee that a resource will be available to an application at runtime is to compile it into the assembly (or referenced assembly).

Referencing a binary resource file...

Resources are referenced using the Pack URI scheme.

From MSDN:
The scheme that is specified by a URI is defined by its prefix; http, ftp, and file are well-known examples. The pack URI scheme uses "pack" as its scheme, and contains two components: authority and path.
The following is the format for a pack URI.
pack://authority/path 
The authority specifies the type of package that a part is contained by, while the path specifies the location of a part within a package.
This concept is illustrated by the following figure:


Packages and parts are analogous to applications and files, where an application (package) can include one or more files (parts)...
To access these types of files, WPF supports two authorities: application:/// and siteoforigin:///.
The application:/// authority identifies application data files that are known at compile time, including resource and content files. The siteoforigin:/// authority identifies site of origin files. The scope of each authority is shown in the following figure.

Application resources are available in the immediate assembly or a referenced assembly.

The syntax for a resource available in the immediate assembly is as follows:
pack://application:,,,/Subfolder/ResourceFile.blah
If the resource resides in a referenced assembly then the assembly name is added, as follows:
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.blah
Note the use of the ';component/' between the assembly name and the path. Also, notice that the authority path is followed by three commas instead of forward slashes.

Next, Logical Resources...

References:

WPF Application Resource, Content, and Data Files: http://msdn.microsoft.com/en-us/library/aa970494.aspx

No comments:

Post a Comment