Skip to content

Instantly share code, notes, and snippets.

@reniris
Last active February 19, 2018 11:52
Show Gist options
  • Save reniris/d4274fb7d85b3690e8c20c1aef8c0669 to your computer and use it in GitHub Desktop.
Save reniris/d4274fb7d85b3690e8c20c1aef8c0669 to your computer and use it in GitHub Desktop.
Xamarin Theme
/// <summary>
/// テーマ変更
/// </summary>
/// <param name="theme">theme</param>
/// <exception cref="NotImplementedException"></exception>
public void ChangeTheme(ThemeType theme)
{
if (MainPage == null) return;
MainPage.Parent = null;
ResourceDictionary t = null;
switch (theme)
{
case ThemeType.Dark:
t = new DarkTheme();
break;
case ThemeType.Light:
t = new LightTheme();
break;
default:
throw new NotImplementedException();
}
Application.Current.Resources.MergedDictionaries.Add(t);
MainPage.Parent = this;
}
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TaxPon.View.Themes.DarkTheme">
<Color x:Key="BackgroundColor">Black</Color>
<Color x:Key="ForegroundColor">#bdc3c7</Color>
<Color x:Key="HeaderBackgroundColor">#20202a</Color>
<Color x:Key="HeaderForegroundColor">#bdc3c7</Color>
<Color x:Key="HeaderLogoColor">#1abc9c</Color>
<Color x:Key="ListItemBackgroundColor">#1c1c1c</Color>
<Color x:Key="ListItemTitleColor">#2980b9</Color>
</ResourceDictionary>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DarkTheme : ResourceDictionary
{
public DarkTheme ()
{
InitializeComponent ();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TaxPon.View.Themes.LightTheme">
<Color x:Key="BackgroundColor">#ecf0f1</Color>
<Color x:Key="ForegroundColor">#33333c</Color>
<Color x:Key="HeaderBackgroundColor">#2c3e50</Color>
<Color x:Key="HeaderForegroundColor">#bdc3c7</Color>
<Color x:Key="HeaderLogoColor">#1abc9c</Color>
<Color x:Key="ListItemBackgroundColor">#bdc3c7</Color>
<Color x:Key="ListItemTitleColor">#2980b9</Color>
</ResourceDictionary>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LightTheme : ResourceDictionary
{
public LightTheme ()
{
InitializeComponent ();
}
}
public enum ThemeType
{
Dark,
Light,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment