After this lesson, you will be able to:
■ Embed a binary resource in an application.
■ Retrieve a binary resource by using code.
■ Retrieve a binary resource by using pack URI syntax.
■ Access a resource in another assembly by using pack URI syntax.
■ Add a content file to an application.
■ Create a resource-only DLL.
■ Load and access a resource-only DLL.
■ Create a logical resource.
■ Create an application resource.
■ Access a resource in XAML.
■ Explain the difference between a static resource and a dynamic resource.
■ Create a resource dictionary.
■ Merge resource dictionaries.
■ Decide where to store a resource.
■ Access a resource object in code.
一、Binary Resources
Binary resources enable you to compile large binary files in your application assemblies and
retrieve them for use in your application. Binary resources are different from logical resources,
which can be defined and accessed in XAML files
1. Embedding Resources
Add the file to your project and set the file’s Build Action property to Resource. When the application is
compiled, the resource is compiled automatically as a resource and embedded in your application
Do not set the Build Action property to Embedded Resource, which embeds the resource
using a different resource management scheme that is less accessible from WPF
applications.
Using embeding Resources
<Image Source="Pics/Image1.jpg"></Image>
<Image Source="Pics\Image1.jpg"></Image>
<Image Source="pack://application:,,,/Pics/Image1.jpg" Margin="17,90,61,22" Name="Image1" Stretch="Fill"/>
Using embeded Resources in other assembly:
pack://application:,,,/<AssemblyName>;component/<Folder>/<FileName>
2. Content Files
Embedding filess would require the application to be recompiled, and the embedding files would require pack uri, so updated frequently files and media MediaPlayer and MediaElement controls do not support the pack URI syntax
 
How to add content files:
Set the Build Action property for this file to Content.
Set Copy To Output Directory property to Copy Always.
 
Using content files
<Image Source="pack://siteOfOrigin:,,,/OfficeFrontDoor.jpg"/>
 
The siteOfOrigin:,,, syntax means different things depending on the location from which
the application was originally installed. If the application is a full-trust desktop application
that was installed using Windows Installer, the siteOfOrigin:,,, syntax in a pack URI refers to
the root directory of the application.
If the application is a full-trust application that was installed using ClickOnce, the
siteOfOrigin:,,,
syntax refers to the Uniform Resource Locator (URL) or the Universal Naming
Convention (UNC) path from which the application was originally deployed.
For a partial-trust application deployed with ClickOnce or an XAML Browser Application
(XBAP), siteOfOrigin:,,, refers to the URL or UNC path that hosts the application.
Pack URIs that use the siteOfOrigin:,,, syntax always point to loose files (that is, files that
are copied to the output directory but are not compiled); they never point to embedded
resources. Thus, the files they reference should always exist in the directory specified by the
siteOfOrigin:,,, syntax in a loose, uncompiled state.
 
 
 
 
 
 
 
 
posted on 2011-06-01 01:20  kuning的程序博客  阅读(162)  评论(0编辑  收藏  举报