WPF - Resources

WPF - RESOURCES
Jim Fawcett, Brown Bag Seminar Series
Fall 2007
Reference
 Windows Presentation Foundation
Unleashed, Adam Nathan, SAMS, 2007
 Note: none of the material in this presentation is
original from me. Everything has been drawn
from Nathan’s book as paraphrases or quotations
without quotes, with in some cases a little
interpretation. Only a diagram or two is original.
Resources
 Binary Resources – used for years
 Bitmaps, strings, compiled XAML
 Logical Resources – New to WPF
 Arbitrary .Net objects named and stored in an
element’s Resources property.
Packaging Binary Resources
 Embedded inside an assembly
 Loose files that are known to the application
at compile time
 Loose files that might not be know to the
application at compile time
Defining Binary Resources
with Visual Studio
 Add file to Visual Studio project and select the
appropriate build action in project properties
 Resource :
Embeds resource into the assembly – may be a culture
specific satellite assembly
 Content:
Leaves the resource as a loose file, but adds a custom
attribute to the assembly that records existence and
relative location of the file.
Accessing Binary Resources
 Access with Uniform Resource Identifier (uri)
 A type converter enables uris to be specified in
XAML as strings.
<Image Height=“21” Source=“previous.gif” />
 The image was included in the Visual Studio
project with build action of Resource.
Localization
 If your application
contains binary
resources that are
specific to certain
cultures, you can
partition them
into satellite
assemblies that
get loaded
automatically.
Single File Assembly
myProject.exe
Multiple File Assembly
myLibrary
lib1.dll
lib2.dll
Manifest
Manifest
Type
Metadata
Type
Metadata
Type
Metadata
MSIL code
MSIL code
MSIL code
optional
resources
lib3.dll
Type
Metadata
lib.bmp
optional
resources
MSIL code
Logical Resources
 New WPF-specific mechanism
 Arbitrary .Net objects stored and named in an
element’s Resources property.
 The base classes FrameworkElement and
FrameworkContentElement both have a Resources
property of type System.Windows.ResourceDictionary
 Most of the WPF classes derive from these base
classes.
 These resources are often used to store styles or
data providers.
Logical Resources
<Window.Resources>
<SolidColorBrush x:Key="backgroundBrush">Yellow</SolidColorBrush>
<SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>
</Window.Resources>
<Window.Background>
<StaticResource ResourceKey="backgroundBrush"/>
</Window.Background>
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"
HorizontalAlignment="Center">
<Button Background="{StaticResource backgroundBrush}"
BorderBrush="{StaticResource borderBrush}" Margin="5">
<Image Height="21" Source="zoom.gif"/>
</Button>
Walking the Resources Tree
 The markup extension class implements the
ability to walk the logical tree to find an item.
 First checks the current element’s Resources




collection.
If not found, it checks the parent element, its parent,
etc, until it reaches the root element.
If all that fails, it looks in the resources collection of
the Application object.
If that fails, it looks at the system default resources
collection.
If that fails, it throws an InvalidOperationException.
Static versus Dynamic Resources
 There are two ways to access a logical
resource:
 Statically with StaticResource, meaning that the
resource is applied only once when it is first
needed.
 Dynamically with DynamicResource, meaning
that the resource is reapplied every time it
changes.
 A consumer of the resource sees changes, e.g., the
resource is linked.
END OF PRESENTATION