1. Computing

Introducing Resources in WPF and XAML

This will get you started with resources in XAML and WPF!

From , former About.com Guide

See More About
Updated December 27, 2009

When you create a new WPF Application, you'll see ...

Application.xaml
   Application.xaml.vb

... in Solution Explorer.

These files don't have much in them initially. Application.xaml just contains:

   <Application.Resources>
      
   </Application.Resources>

Application.xaml.vb doesn't have anything in it except the Class header.

In this article, we're going to introduce one of the many flavors of markup extensions: resources.

Since the key to using application resources in WPF is "markup extensions" let's make sure we're all on the same page about that. XAML is a type of XML so it has to follow the rules of XML. In particular, everything in XML must be a text string. So, if you want to put what is essentially "declarative program code" into XML, you have to figure out a way of doing that using a text string. Microsoft's way is to allow you to use what they call "markup extensions". In other words, they "extend" the ways that XML markup can be used. If you have tried my WPF introduction, markup extensions are used in Part 3 and Part 4.

---------

Side note: One of the criticisms hurled at Microsoft over the years was that they lock you into their products with an "embrace and extend" strategy. In other words, they first "embrace" a standard technology and then they "extend" that technology in proprietary ways. After you have invested in development using their technologies, you can't afford to switch. "Extending" XML markup appears to be a prime example, even though it is a great technology. Standards advocates want Microsoft to stick to using only technologies that anybody else can also use. Although in theory, Microsoft is deflecting this criticism by placing XAML under what is called the Open Specification Promise - they promise not to sue anyone using these specific technologies - in practice, you're only likely to see a full implementation of XAML in a Microsoft product.

---------

Since they have to do it all in text, Microsoft's XAML "markup extensions" use the rule that any XAML attribute totally enclosed in curly braces - "{ ... }" - will be interpreted as a markup extension. Following the pattern for .NET, Microsoft supplies a fairly long list of markup extensions you can use but if that isn't enough, you can also code your own. We're going to look at just one way to use markup extensions: resources. (Just as clarification, the term "resources" can also refer to files like images or videos used by the application. That type of resource is not what this is about.)

As a starting example of a resource used in a WPF application, let's simply make the Fill property of a Rectangle About.com orange. Step 1 could be to add the resource to the Application.xaml file:

<Window x:Class="Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Window1" Height="300" Width="300">
      <Rectangle Height="100"
            Name="myRect"
            Width="150"
            Fill="{StaticResource AboutLogoColor}"/>
</Window>

This gives you the OOP advantage of being able to change the color in just one place no matter how often it's used in the code, if that's ever necessary. (It happens! Trust me on that.)

On the next page, some of the specific rules that apply to XAML resources are introduced.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Introducing Application Resources in WPF and XAML in a Visual Basic .NET context

©2013 About.com. All rights reserved.