1 / 13

WPF - Resources

Jim Fawcett, Brown Bag Seminar Series Fall 2007. WPF - Resources. Reference. Windows Presentation Foundation Unleashed, Adam Nathan, SAMS, 2007

Download Presentation

WPF - Resources

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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> <StaticResourceResourceKey="backgroundBrush"/> </Window.Background> <DockPanel> <StackPanelDockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Background="{StaticResourcebackgroundBrush}" BorderBrush="{StaticResourceborderBrush}" Margin="5"> <Image Height="21" Source="zoom.gif"/> </Button>

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

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

  12. End of Presentation

More Related