views:

69

answers:

1

Hi:

I'm making an app for windows phone 7 (using Visual Studio 2010 Express for Windows Phone RTM), and i'm having some problems regarding theme (light/dark) awareness especially with colors.

Basically i want to do the same thing that the phone does internally when using the staticresource colors/brushes. For example, if i declare:

<Border Background="{StaticResource PhoneBackgroundBrush}" />

The Border will have a black background with the Dark Theme and White with the Light one. So, i want to have a resource ... let's say MyBackgroundBrush, and will be used like:

<Border Background="{StaticResource MyBackgroundBrush}" />

And, it will have, maybe blue for the Dark theme, and red for the light.

So my question is, how can i define such resource and behavior?

A: 

See this question about theme detection. That would get you a way to detect the theme, then use that information to set your background brush by replacing it in the app's resources:

App.Current.Resources.Remove("MyPhoneBrush");
App.Current.Resources.Add("MyPhoneBrush", value);

You could define both of the colored brushes with a name in xaml, then set one or the other as the runtime value of MyPhoneBrush. you'd probably want to set one of them as the default so things work at design time too...

John Gardner
Thanks for your answer. I think i understand the idea but i'm unable to test it right now. Where should i declare your code?Will it work in App.xaml.cs, like this: this.Current.Resources.Add("MyPhoneBrush", value);?
Felipe Guajardo
App.Current is static, so you should be able to put that code pretty much anywhere. if you're basing stuff on one of the standard templates, you could put it in MainPage_Loaded, where (depending on the project) there should already be code referencing App.
John Gardner
Thanks, i just made it work a while ago. One thing to note though, it might have been due to my rudimentary implementation, but it matters where the code is declared. For example, i had a texblock style definition in App.xaml. Then i declared your code in App.xaml.cs->Application_Launching(s,e). After that, i changed the Foreground property in my style to the static resource, but didn't work since (i guess) App.Xaml gets parsed before the resource gets added. Still, i got it to work by overriding Foreground directly in the textblock with the staticresource.
Felipe Guajardo