Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Created September 22, 2016 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/c836c6706b9da12b914f00f2125831bc to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/c836c6706b9da12b914f00f2125831bc to your computer and use it in GitHub Desktop.
Custom Themeing model for Xamarin Forms' DynamicResource
[DataContract]
public class ThemeModel : INotifyPropertyChanged
{
private Color backgroundColor;
private Color textColor;
private Color buttonBackgroundColor;
private Color buttonTextColor;
private Color accentColor;
private Color headerColor;
public ThemeModel()
{
}
public ThemeModel(Color backgroundColor, Color textColor, Color buttonBackgroundColor, Color buttonTextColor, Color accentColor, Color headerColor)
{
this.backgroundColor = backgroundColor;
this.textColor = textColor;
this.buttonBackgroundColor = buttonBackgroundColor;
this.buttonTextColor = buttonTextColor;
this.accentColor = accentColor;
this.headerColor = headerColor;
}
[DataMember]
public Color BackgroundColor
{
get { return backgroundColor; }
set { backgroundColor = value; OnPropertyChanged();}
}
[DataMember]
public Color TextColor
{
get { return textColor; }
set { textColor = value; OnPropertyChanged();}
}
[DataMember]
public Color ButtonBackgroundColor
{
get { return buttonBackgroundColor; }
set { buttonBackgroundColor = value; OnPropertyChanged();}
}
[DataMember]
public Color ButtonTextColor
{
get { return buttonTextColor; }
set { buttonTextColor = value; OnPropertyChanged();}
}
[DataMember]
public Color AccentColor
{
get { return accentColor; }
set { accentColor = value; OnPropertyChanged();}
}
[DataMember]
public Color HeaderColor
{
get { return headerColor; }
set { headerColor = value; OnPropertyChanged();}
}
public bool GetCurrentTheme()
{
try
{
BackgroundColor = (Color) App.Current.Resources["ThemeBackgroundColor"];
TextColor = (Color) App.Current.Resources["ThemeTextColor"];
ButtonBackgroundColor = (Color) App.Current.Resources["ThemeButtonBackgroundColor"];
ButtonTextColor = (Color) App.Current.Resources["ThemeButtonTextColor"];
AccentColor = (Color) App.Current.Resources["ThemeAccentColor"];
HeaderColor = (Color) App.Current.Resources["ThemeHeaderColor"];
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"ThemeModel.GetCurrentTheme Exception: {ex}");
return false;
}
}
public bool SetCurrentTheme()
{
try
{
App.Current.Resources["ThemeBackgroundColor"] = BackgroundColor;
App.Current.Resources["ThemeTextColor"] = TextColor;
App.Current.Resources["ThemeButtonBackgroundColor"] = ButtonBackgroundColor;
App.Current.Resources["ThemeButtonTextColor"] = ButtonTextColor;
App.Current.Resources["ThemeAccentColor"] = AccentColor;
App.Current.Resources["ThemeHeaderColor"] = HeaderColor;
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"ThemeModel.SetCurrentTheme Exception: {ex}");
return false;
}
}
public bool ResetToDefaultTheme()
{
try
{
BackgroundColor = Color.White;
TextColor = Color.Black;
ButtonBackgroundColor = Color.Silver;
ButtonTextColor = Color.White;
AccentColor = Color.FromHex("#FFEF322F");
HeaderColor = Color.FromHex("#D8D7D7");
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"ThemeModel.SetCurrentTheme Exception: {ex}");
return false;
}
}
public void Clear()
{
//note to self: using properties so that prop changed is fired
BackgroundColor = new Color();
TextColor = new Color();
ButtonBackgroundColor = new Color();
ButtonTextColor = new Color();
AccentColor = new Color();
HeaderColor = new Color();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
@LanceMcCarthy
Copy link
Author

IMPORTANT : If you're going to use this directly, make sure you have the resource defined in your App.xaml file

<Color x:Key="ThemeBackgroundColor">White</Color>
      <Color x:Key="ThemeTextColor">Black</Color>
      <Color x:Key="ThemeButtonBackgroundColor">Silver</Color>
      <Color x:Key="ThemeButtonTextColor">White</Color>
      <Color x:Key="ThemeAccentColor">#FFEF322F</Color>
      <Color x:Key="ThemeHeaderColor">#D8D7D7</Color>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment