アプリの中で白背景と黒背景を切り替える


#wp7dev_jp

さて、大詰めです。テーマ設定に関係なく、アプリの中で白背景と黒背景を切り替える方法です。

■ 簡易版

画面をタップすると白と黒が入れ替わります。

MainPage.xaml.cs

Boolean bLight = false;

private void LayoutRoot_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    bLight = !bLight;
    if (bLight)
    {
        (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current as App).RootFrame.Background = new SolidColorBrush(Colors.Black);
    }
    else
    {
        (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current as App).RootFrame.Background = new SolidColorBrush(Colors.White);
    }
}

imageimage

しかし、コントロールの背景の一部などは色がなくなってしまう場合があります。それはそれぞれ色が別に設定されているためです。そこで完全版。テーマリソースで定義されている色設定を全てマニュアルで定義します。

■ 完全版

MainPage.xaml.cs

Boolean bLight = false;

private void LayoutRoot_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    bLight = !bLight;
    if (bLight)
        Theme.SetLightTheme();
    else
        Theme.SetDarkTheme();
}

Theme.cs

public static class Theme
{
    public static void SetLightTheme()
    {
        (App.Current as App).RootFrame.Background = new SolidColorBrush(Colors.White);

        (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneContrastBackgroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneContrastBackgroundBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneDisabledBrush"] as SolidColorBrush).Color = Color.FromArgb(77, 0, 0, 0);

        (App.Current.Resources["PhoneTextCaretBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneTextBoxBrush"] as SolidColorBrush).Color = Color.FromArgb(38, 0, 0, 0);
        (App.Current.Resources["PhoneTextBoxForegroundBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneTextBoxEditBackgroundBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneTextBoxEditBorderBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneTextBoxReadOnlyBrush"] as SolidColorBrush).Color = Color.FromArgb(46, 0, 0, 0);
        (App.Current.Resources["PhoneSubtleBrush"] as SolidColorBrush).Color = Color.FromArgb(102, 0, 0, 0);
        //(App.Current.Resources["PhoneTextBoxSelectionForegroundBrush"] as SolidColorBrush).Color = Colors.White;

        (App.Current.Resources["PhoneRadioCheckBoxBrush"] as SolidColorBrush).Color = Color.FromArgb(38, 0, 0, 0);
        (App.Current.Resources["PhoneRadioCheckBoxDisabledBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneRadioCheckBoxCheckBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);
        (App.Current.Resources["PhoneRadioCheckBoxCheckDisabledBrush"] as SolidColorBrush).Color = Color.FromArgb(77, 0, 0, 0);
        (App.Current.Resources["PhoneRadioCheckBoxPressedBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneRadioCheckBoxPressedBorderBrush"] as SolidColorBrush).Color = Color.FromArgb(222, 0, 0, 0);

        (App.Current.Resources["PhoneSemitransparentBrush"] as SolidColorBrush).Color = Color.FromArgb(170, 255, 255, 255);
        (App.Current.Resources["PhoneChromeBrush"] as SolidColorBrush).Color = Color.FromArgb(255, 221, 221, 221);

        (App.Current.Resources["PhoneInactiveBrush"] as SolidColorBrush).Color = Color.FromArgb(51, 0, 0, 0);
        (App.Current.Resources["PhoneInverseInactiveBrush"] as SolidColorBrush).Color = Color.FromArgb(255, 229, 229, 229);
        (App.Current.Resources["PhoneInverseBackgroundBrush"] as SolidColorBrush).Color = Color.FromArgb(255, 221, 221, 221);
        (App.Current.Resources["PhoneBorderBrush"] as SolidColorBrush).Color = Color.FromArgb(153, 0, 0, 0);

    }

    public static void SetDarkTheme()
    {

         (App.Current as App).RootFrame.Background = new SolidColorBrush(Colors.Black);

        (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneContrastBackgroundBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneContrastBackgroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneDisabledBrush"] as SolidColorBrush).Color = Color.FromArgb(102, 255, 255, 255);

        (App.Current.Resources["PhoneTextCaretBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneTextBoxBrush"] as SolidColorBrush).Color = Color.FromArgb(191, 255, 255, 255);
        (App.Current.Resources["PhoneTextBoxForegroundBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneTextBoxEditBackgroundBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneTextBoxEditBorderBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneTextBoxReadOnlyBrush"] as SolidColorBrush).Color = Color.FromArgb(119, 0, 0, 0);
        (App.Current.Resources["PhoneSubtleBrush"] as SolidColorBrush).Color = Color.FromArgb(153, 255, 255, 255);
        //(App.Current.Resources["PhoneTextBoxSelectionForegroundBrush"] as SolidColorBrush).Color = Colors.White;

        (App.Current.Resources["PhoneRadioCheckBoxBrush"] as SolidColorBrush).Color = Color.FromArgb(191, 255, 255, 255);
        (App.Current.Resources["PhoneRadioCheckBoxDisabledBrush"] as SolidColorBrush).Color = Color.FromArgb(102, 255, 255, 255);
        (App.Current.Resources["PhoneRadioCheckBoxCheckBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneRadioCheckBoxCheckDisabledBrush"] as SolidColorBrush).Color = Color.FromArgb(102, 0, 0, 0);
        (App.Current.Resources["PhoneRadioCheckBoxPressedBrush"] as SolidColorBrush).Color = Colors.White;
        (App.Current.Resources["PhoneRadioCheckBoxPressedBorderBrush"] as SolidColorBrush).Color = Colors.White;

        (App.Current.Resources["PhoneSemitransparentBrush"] as SolidColorBrush).Color = Color.FromArgb(170, 0, 0, 0);
        (App.Current.Resources["PhoneChromeBrush"] as SolidColorBrush).Color = Color.FromArgb(255, 31, 31, 31);

        (App.Current.Resources["PhoneInactiveBrush"] as SolidColorBrush).Color = Color.FromArgb(51, 255, 255, 255);
        (App.Current.Resources["PhoneInverseInactiveBrush"] as SolidColorBrush).Color = Color.FromArgb(255, 204, 204, 204);
        (App.Current.Resources["PhoneInverseBackgroundBrush"] as SolidColorBrush).Color = Colors.Black;
        (App.Current.Resources["PhoneBorderBrush"] as SolidColorBrush).Color = Color.FromArgb(191, 255, 255, 255);
    }
}

1個所コメントアウトしているのは、白背景時も黒背景時も特に設定が変わらないため。

imageimage

チェックボックスもテイストボックスもスライダーも完璧です。

テーマで白黒切り替えるのは面倒ですが、アプリ内に白・黒切り替える機能があればちょっとうれしいかもしれません。

Comments (0)

Skip to main content