How to define and reuse resources in WP7
published on: 2/23/2011 | Views: N/A | Tags: Resources
by WindowsPhoneGeek
In this quick tip I am going to give some examples of how to define/access/use resources either in XMAL or C# in Windows Phone 7 application.
How to define and access a reusable resource?
You can define any resource: style, brush, color, etc. in the Resources section of every FrameworkElement. In XAML, you can establish resource items as child object elements of a FrameworkElement.Resources property element, through XAML implicit collection syntax. Note that you have to set any Key so that you will be able to acces the resource by its key.
<phone:PhoneApplicationPage.Resources>
<SolidColorBrush Color="YellowGreen" x:Key="greenColor"/>
</phone:PhoneApplicationPage.Resources>
You can access the resources either in XAML or C# in the following way:
XAML:
<Rectangle Height="80" Width="80" Fill="{StaticResource greenColor}"/>
StaticResource is a technique for obtaining values that are defined elsewhere in a resource dictionary. each value must have a particular x:Key name. This key is initially assigned by the x:Key attribute on a resource that is defined in a XAML ResourceDictionary.
NOTE: Attempting to specify a StaticResource to a key that cannot resolve results in a XAML parse exception.
C#:
Rectangle rect = new Rectangle() {Height=80,Width=80};
rect.Fill = this.Resources["greenColor"] as SolidColorBrush;
this.ContentPanel.Children.Add(rect);
How to use the existing WP7 Theme resource?
By default Windows Phone uses a resource dictionary for theme resources, a keyed dictionary of objects that can be used both in XAML and in code. You can get any resource from the dictionary either in XAML or C# by using for example:
XAML:
<Rectangle Height="80" Width="80" Fill="{StaticResource PhoneAccentBrush}"/>
C#:
rect.Fill = App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
or
rect.Fill = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
NOTE: Here is a list of all available theme resources in Windows Phone 7: Theme Resources for Windows Phone
I hope that these tips were helpful.
You can also follow us on Twitter @winphonegeek
Comments
thanks!
posted by: Luke Venediger on 2/24/2011 4:29:47 AM
Useful info - thanks!
builds ok, but colors don't change
posted by: RickH on 4/15/2011 8:17:07 AM
I have this in App.xaml:
This in MainPage.xaml.cs: LayoutRoot.Background = this.Resources["darkBlueColor"] as SolidColorBrush;
Color doesn't change when I run it.
I'm reduced to using: LayoutRoot.Background = new SolidColorBrush(Colors.Blue); which has a very limited range of colors.
builds, but no color changes
posted by: rickhan@tirnanog.com on 4/15/2011 8:22:00 AM
I have this in App.xaml:
This in MainPage.xaml.cs: LayoutRoot.Background = this.Resources["darkBlueColor"] as SolidColorBrush;
Color doesn't change when I run it.
I'm reduced to using: LayoutRoot.Background = new SolidColorBrush(Colors.Blue); which has a very limited range of colors
comments keep truncation 1st half of what I type
posted by: rickhan@tirnanog.com on 4/15/2011 8:24:36 AM
I have this in App.xaml:
This in MainPage.xaml.cs: LayoutRoot.Background = this.Resources["darkBlueColor"] as SolidColorBrush;
Color doesn't change when I run it.
I'm reduced to using: LayoutRoot.Background = new SolidColorBrush(Colors.Blue); which has a very limited range of colors
Re: builds ok, but colors don't change
posted by: winphonegeek on 4/26/2011 9:52:58 PM
If you want to access a resource placed inside App.xaml then you have to use the following code instead:
this.LayoutRoot.Background = App.Current.Resources["greenColor"] as SolidColorBrush;
Where "greenColor" is a brush defined in App.xaml:
<Application.Resources>
<SolidColorBrush Color="YellowGreen" x:Key="greenColor"/>
</Application.Resources>
what is "this" referring to?
posted by: dd on 3/16/2012 7:26:27 AM
The first c# code example uses "this.Resources". Maybe this is a newbie question, but what is "this"? That is, what class are you assuming this code lies within?
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
