如何解决如何在 UWP 和 WinUi 中更改应用程序主题
我想更改我的 UWP (winui 2.5) App 主题,我使用了以下代码
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 控件库示例项目中复制了代码
这是运行良好的微软示例
解决方法
安装 Microsoft Toolkit 并使用扩展程序
maps = []
for object in obj:
maps.append(Map(object[0],object[1],object[2]))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。