Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

WPF: What is a Resource?

.. it certainly ain't an embedded resource in an assembly.

Posted on 2/13/2007 @ 4:18 PM in #WPF | 1 comments | 4761 views

So what is a resource? Is it something in your project, whose properties has something called "Build Action", and you change that to "Embedded Resource" and then you don't need to ship 20 files with your DLL? Yeah, that is an embedded resource, and I've talked previously on my blog about how to use that.

That is not what this blog post is about however. This is about a "Resource in WPF".

What is a resource in WPF?

A resource in WPF is a quick and easy way to re-use commonly defined objects and values over your XAML or in general WPF code. Here is an example. Say for instance, you were writing a XAML flow document as follows

<FlowDocument FontSize="14" FontFamily="Georgia">
  <Paragraph>
    <TextBlock>
      All about Resources in WPF
    </TextBlock>
    <TextBlock>
      This blogpost talks about what resources are in WPF.
    </TextBlock>
    <TextBlock>
      What this blog post doesn't talk about however, is databinding.
    </TextBlock>
  </Paragraph>
</FlowDocument>

When you render the above in a suitable container, the three textblocks look exactly the same. This can be seen as below:

But from the content, it is clear that the first textblock needs to be formatted differently - afterall, it is a heading. So you could suitably add font, color etc. to the individual tags, but in C# you have the flexibility of declaring constants or statics in a class, and use them all over your code. By isolating such variables at a central and easy to maintain place, you make your code easier to use.

Resources are almost on the same theme. They let you isolate and re-use commonly defined objects and values. To view this in action, just modify the above XAML to include resources like this -

<FlowDocument FontSize="14" FontFamily="Georgia">
  <FlowDocument.Resources>
    <SolidColorBrush x:Key="Heading" Color="Red"/>
    <SolidColorBrush x:Key="Content" Color="Blue"/>
  </FlowDocument.Resources>
  <Paragraph>
    <TextBlock Foreground="{StaticResource Heading}">
      All about Resources in WPF
    </TextBlock>
    <TextBlock Foreground="{StaticResource Content}">
      This blogpost talks about what resources are in WPF.
    </TextBlock>
    <TextBlock Foreground="{StaticResource Content}">
      What this blog post doesn't talk about however, is databinding.
    </TextBlock>
  </Paragraph>
</FlowDocument>

This, will identify the heading somewhat differently in Red color as shown below:

As you can see, now if I wished to change all my content from "Blue" to "Green", I'd have to make only one change to my XAML file, and that would be -

<SolidColorBrush x:Key="Content" Color="Green"/>

Here is the resultant Window:

Clearly, this alone isn't that useful. You probably want to set both background, and foreground, maybe even font size, or even other things (3d effects?) that will delineate one part of your XAML from another. You can group these resources in a "Style" and get functionality a.k.a. Stylesheets in XAML.


On 6/5/2008 12:25:55 AM proxyvon said ..
Nice one...

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.