MaterialDesign 运行时更改主题 Wpf
MaterialDesign Runtime Change Theme Wpf
我想在运行时更改我的项目的主题。我希望它是深色或浅色模式。我正在使用材料设计。运行时主题发生变化,但我无法控制颜色。我错过了什么?
我的App.xaml代码
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesignLightTheme.xaml"/>
<!-- primary colors -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#244886"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesignLightTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#244886"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我的MaterialDesignDarkTheme.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestMaterialExtensions.Themes"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
>
<ResourceDictionary.MergedDictionaries>
<!-- theme from MaterialDesignInXAML -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml"/>
<!-- additional resources -->
<ResourceDictionary>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="Red"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
我的MaterialDesignLightTheme.xaml
<ResourceDictionary.MergedDictionaries>
<!-- theme from MaterialDesignInXAML -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<!-- own themes -->
<!-- additional resources -->
<ResourceDictionary>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="Yellow"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
我的更改主题代码
public void SetLightDark(bool isDark)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MaterialDesignExtensions;component\/Themes\/MaterialDesign((Light)|(Dark))Theme)").Success);
var source = $"pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
我解决了这个问题。问题是由于我没有在SetLightDark中正确设置我的xaml文件的地址路径,所以我创建的地址文件根本无法访问。
更改ThemeChange.cs代码
public void SetLightDark(bool isDark)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/Themes\/MaterialDesign((Light)|(Dark))Theme)").Success);
var source = $"pack://application:,,,/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
以前的路径
$"pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml"
\/MaterialDesignExtensions;component\/Themes\/MaterialDesign((Light)|(Dark))Theme)"
当前路径
@"(\/Themes\/MaterialDesign((Light)|(Dark))Theme)"
$"pack://application:,,,/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml"
...我的theme.xaml文件在项目下名为Themes的文件夹中
我想在运行时更改我的项目的主题。我希望它是深色或浅色模式。我正在使用材料设计。运行时主题发生变化,但我无法控制颜色。我错过了什么?
我的App.xaml代码
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesignLightTheme.xaml"/>
<!-- primary colors -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#244886"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesignLightTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#244886"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我的MaterialDesignDarkTheme.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestMaterialExtensions.Themes"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
>
<ResourceDictionary.MergedDictionaries>
<!-- theme from MaterialDesignInXAML -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml"/>
<!-- additional resources -->
<ResourceDictionary>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="Red"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
我的MaterialDesignLightTheme.xaml
<ResourceDictionary.MergedDictionaries>
<!-- theme from MaterialDesignInXAML -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<!-- own themes -->
<!-- additional resources -->
<ResourceDictionary>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="Yellow"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
我的更改主题代码
public void SetLightDark(bool isDark)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MaterialDesignExtensions;component\/Themes\/MaterialDesign((Light)|(Dark))Theme)").Success);
var source = $"pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
我解决了这个问题。问题是由于我没有在SetLightDark中正确设置我的xaml文件的地址路径,所以我创建的地址文件根本无法访问。
更改ThemeChange.cs代码
public void SetLightDark(bool isDark)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/Themes\/MaterialDesign((Light)|(Dark))Theme)").Success);
var source = $"pack://application:,,,/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
以前的路径
$"pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml"
\/MaterialDesignExtensions;component\/Themes\/MaterialDesign((Light)|(Dark))Theme)"
当前路径
@"(\/Themes\/MaterialDesign((Light)|(Dark))Theme)"
$"pack://application:,,,/Themes/MaterialDesign{(isDark ? "Dark" : "Light")}Theme.xaml"
...我的theme.xaml文件在项目下名为Themes的文件夹中