Created
September 22, 2016 20:38
-
-
Save LanceMcCarthy/c836c6706b9da12b914f00f2125831bc to your computer and use it in GitHub Desktop.
Custom Themeing model for Xamarin Forms' DynamicResource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IMPORTANT : If you're going to use this directly, make sure you have the resource defined in your App.xaml file