Skip to content

Instantly share code, notes, and snippets.

@jasonmitchell
Created January 5, 2013 17:34
Show Gist options
  • Save jasonmitchell/4462646 to your computer and use it in GitHub Desktop.
Save jasonmitchell/4462646 to your computer and use it in GitHub Desktop.
<local:ThemeAwarePanorama Title="my app"
LightBackground="/MyApp;component/Content/Images/Light/Background.jpg"
DarkBackground="/MyApp;component/Content/Images/Dark/Background.jpg">
<!-- more xaml -->
</local:ThemeAwarePanorama>
public class ThemeAwarePanorama : Panorama
{
public static readonly DependencyProperty LightBackgroundProperty = DependencyProperty.Register("LightBackground", typeof (string), typeof (ThemeAwarePanorama), new PropertyMetadata(default(string)));
public static readonly DependencyProperty DarkBackgroundProperty = DependencyProperty.Register("DarkBackground", typeof (string), typeof (ThemeAwarePanorama), new PropertyMetadata(default(string)));
public ThemeAwarePanorama()
{
Loaded += (sender, args) =>
{
if (!string.IsNullOrEmpty(LightBackground) && !string.IsNullOrEmpty(DarkBackground))
{
string url = UIHelper.IsLightThemeEnabled() ? LightBackground : DarkBackground;
Background = new ImageBrush {ImageSource = new BitmapImage(new Uri(url, UriKind.Relative))};
}
else
{
throw new InvalidOperationException("Light and dark background must be provided");
}
};
}
public string LightBackground
{
get { return (string) GetValue(LightBackgroundProperty); }
set { SetValue(LightBackgroundProperty, value); }
}
public string DarkBackground
{
get { return (string)GetValue(DarkBackgroundProperty); }
set { SetValue(DarkBackgroundProperty, value); }
}
}
public static class UIHelper
{
public static bool IsLightThemeEnabled()
{
return (Visibility) Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment