MahApps.Metro icon indicating copy to clipboard operation
MahApps.Metro copied to clipboard

ChangeAppStyle only works once

Open DerKiLLa opened this issue 7 years ago • 6 comments

I try to implement a Light/Dark-Switch. When i press the button, ChangeAppStyle works once. When i press it again, nothing happens. No error, no change (only the background). It doesn't matter, which Theme i choose ... if i use BaseDark in App.xaml, i can change once to BaseLight, but not back to BaseDark.

 private void lightDARK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MainWindow mw = (MainWindow)Application.Current.MainWindow;
                SolidColorBrush color;
                if (mw.Background.ToString() == "#FF696969")
                {
                    ThemeManager.ChangeAppStyle(this, ThemeManager.GetAccent("Blue"), ThemeManager.GetAppTheme("BaseLight"));
                    color = Brushes.LightGray;
                }
                else
                {
                    ThemeManager.ChangeAppStyle(this, ThemeManager.GetAccent("Blue"), ThemeManager.GetAppTheme("BaseDark"));
                    color = Brushes.DimGray;
                }
                mw.Background = color;
            }

            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
        }

DerKiLLa avatar Jul 02 '19 09:07 DerKiLLa

@DerKiLLa Are you really have all necessary resources in your App.xaml? Like

<Application x:Class="WpfApp4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp4"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

punker76 avatar Jul 02 '19 09:07 punker76

mahapps_change_theme_sample

punker76 avatar Jul 02 '19 09:07 punker76

Your sample shows exactly my behaviour. At first, u have a bright button (Theme: BaseLight), then u click several times the button and the button stays dark (Theme: BaseDark). Only the background changes.

app.xaml:

<Application x:Class="zTiredV2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:zTiredV2"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

DerKiLLa avatar Jul 02 '19 09:07 DerKiLLa

@DerKiLLa Ah, sorry, my mistake. You have 2 options to fix this.

  1. use the current Application to change the theme, because you initialize it at the App.xaml
ThemeManager.ChangeAppStyle(Application.Current, .... );
  1. add the accent and theme resource to the Window resources when you use the ChangeSppStyle method with this (the Window), because the Window need also the accent and theme initialization then.
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

punker76 avatar Jul 02 '19 09:07 punker76

mahapps_change_theme_sample_2

punker76 avatar Jul 02 '19 09:07 punker76

Thanks very much! Application.Current works perfectly! :)

DerKiLLa avatar Jul 02 '19 09:07 DerKiLLa