Saturday, October 2, 2010

Windows Phone 7 Quick Tip #12 – Metro Part II – Built in Resources

In our previous quick tip we made the point that using the built in color theme is really an all or nothing decision.  This quick tip will discuss some of the built-in resources that allow you to follow those built in theme.

Microsoft has kindly published a list that lists all of the resources in the built in resource dictionary that are available to us developers.  Here is a quick over view on how to apply resources to your Windows Phone application.

It’s fairly trivial on how to apply any of these resources into your XAML, simply reference them as a static resource like

<TextBlock Text=”Hello Windows Phone” Foreground=”{StaticResource PhoneForegroundColor}” />

Sometimes you need to build up some of the controls for your page in the code behind section.  If so you can still access the resources.  It’s still simple, but not quite as obvious.  You can get access to the resources with the following chunk of code:

var backgroundColor = (Color)Application.Current.Resources["PhoneBackgroundColor"];

Finally you should be aware that when you create a new page or user control in Visual Studio 2010 with a built in template it will automatically help you out by add the resources to the UserControl or PhoneApplicationPage definition such as:

<phone:PhoneApplicationPage
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"

In summary, using built in resources helps you with your primary goal of making your application feel like it belongs on the phone.

-twb

No comments:

Post a Comment