by vbandi via VBandi's blog on 1/29/2012 12:00:31 PM
The built-in resources in Windows Phone 7 help a lot with automatically reflecting the selected theme of the phone. (See my earlier blog post: Being theme aware in Windows Phone 7 Silverlight apps). However, sometimes you want to be theme aware, but need more than the predefined resources. For example, you may want to have a generally blue background color, but want it to be a dark blue for the dark theme, and a light blue for the light theme. I created a little Behavior that allows you to do just this.
Here is how to use it:
… And when you launch the app, you will see that it works as desired:
Based on the theme detection logic by Bart Wullems, here is the code for the behavior:
public class ThemeToStateBehavior : Behavior<Control>
{
protected override void OnAttached()
base.OnAttached();
var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] ==
Visibility.Visible;
Dispatcher.BeginInvoke(() =>
VisualStateManager.GoToState(AssociatedObject, isLightTheme ? LightState : DarkState,
true));
}
[CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
public string LightState { get; set; }
public string DarkState{ get; set; }
Enjoy!
Original Post: WP7 – Changing Visuals Based on Phone Theme with ThemeToStateBehavior
The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.