如何在 UWP 和 WinUi 中更改应用程序主题
How to Change Application Theme in UWP and WinUi
我想更改我的UWP (winui 2.5) 应用程序主题,我使用了以下代码
private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e)
{
var selectedTheme = ((RadioButton)sender)?.Tag?.ToString();
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
if (selectedTheme != null)
{
App.RootTheme = App.GetEnum<ElementTheme>(selectedTheme);
if (selectedTheme == "Dark")
{
titleBar.ButtonForegroundColor = Colors.White;
}
else if (selectedTheme == "Light")
{
titleBar.ButtonForegroundColor = Colors.Black;
}
else
{
if (Application.Current.RequestedTheme == ApplicationTheme.Dark)
{
titleBar.ButtonForegroundColor = Colors.White;
}
else
{
titleBar.ButtonForegroundColor = Colors.Black;
}
}
}
}
并在 App.xaml.cs
public static ElementTheme RootTheme
{
get
{
if (Window.Current.Content is FrameworkElement rootElement)
{
return rootElement.RequestedTheme;
}
return ElementTheme.Default;
}
set
{
if (Window.Current.Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = value;
}
}
}
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
{
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
}
return (TEnum)Enum.Parse(typeof(TEnum), text);
}
但结果不是预期的如您所见,按钮在更改主题时出现问题我从 Microsoft Xaml Controls Gallery 示例项目
复制了代码
这是 Microsoft 示例,运行良好
安装Microsoft Toolkit并使用扩展
toolkit:TitleBarExtensions.ButtonBackgroundColor="Transparent"
toolkit:TitleBarExtensions.ButtonInactiveBackgroundColor="Transparent"
我想更改我的UWP (winui 2.5) 应用程序主题,我使用了以下代码
private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e)
{
var selectedTheme = ((RadioButton)sender)?.Tag?.ToString();
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
if (selectedTheme != null)
{
App.RootTheme = App.GetEnum<ElementTheme>(selectedTheme);
if (selectedTheme == "Dark")
{
titleBar.ButtonForegroundColor = Colors.White;
}
else if (selectedTheme == "Light")
{
titleBar.ButtonForegroundColor = Colors.Black;
}
else
{
if (Application.Current.RequestedTheme == ApplicationTheme.Dark)
{
titleBar.ButtonForegroundColor = Colors.White;
}
else
{
titleBar.ButtonForegroundColor = Colors.Black;
}
}
}
}
并在 App.xaml.cs
public static ElementTheme RootTheme
{
get
{
if (Window.Current.Content is FrameworkElement rootElement)
{
return rootElement.RequestedTheme;
}
return ElementTheme.Default;
}
set
{
if (Window.Current.Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = value;
}
}
}
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
{
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
}
return (TEnum)Enum.Parse(typeof(TEnum), text);
}
但结果不是预期的如您所见,按钮在更改主题时出现问题我从 Microsoft Xaml Controls Gallery 示例项目
复制了代码这是 Microsoft 示例,运行良好
安装Microsoft Toolkit并使用扩展
toolkit:TitleBarExtensions.ButtonBackgroundColor="Transparent"
toolkit:TitleBarExtensions.ButtonInactiveBackgroundColor="Transparent"