Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jim Fawcett, Brown Bag Seminar Series Fall 2007

Similar presentations


Presentation on theme: "Jim Fawcett, Brown Bag Seminar Series Fall 2007"— Presentation transcript:

1 Jim Fawcett, Brown Bag Seminar Series Fall 2007
WPF - Resources Jim Fawcett, Brown Bag Seminar Series Fall 2007

2 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.

3 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.

4 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

5 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.

6 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.

7 Localization If your application contains binary resources that are specific to certain cultures, you can partition them into satellite assemblies that get loaded automatically.

8 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.

9 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>

10

11 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.

12 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.

13 End of Presentation


Download ppt "Jim Fawcett, Brown Bag Seminar Series Fall 2007"

Similar presentations


Ads by Google