correct theme gets applied when windows theme is changed while the app is running
This commit is contained in:
Родитель
ec70cfb66f
Коммит
55756289ab
|
@ -7,6 +7,7 @@ namespace Param_RootNamespace;
|
|||
public partial class App : Application
|
||||
{
|
||||
public static WindowEx MainWindow { get; } = new MainWindow();
|
||||
public static UIElement? AppTitlebar;
|
||||
|
||||
public App()
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.UI;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
|
@ -97,4 +97,24 @@ internal class TitleBarHelper
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplySystemThemeToCaptionButtons()
|
||||
{
|
||||
var res = Application.Current.Resources;
|
||||
var frame = App.AppTitlebar as FrameworkElement;
|
||||
if (frame != null)
|
||||
{
|
||||
if (frame.ActualTheme == ElementTheme.Dark)
|
||||
{
|
||||
res["WindowCaptionForeground"] = Colors.White;
|
||||
}
|
||||
else
|
||||
{
|
||||
res["WindowCaptionForeground"] = Colors.Black;
|
||||
}
|
||||
|
||||
UpdateTitleBar(frame.ActualTheme);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
using Param_RootNamespace.Helpers;
|
||||
|
||||
using Windows.UI.ViewManagement;
|
||||
namespace Param_RootNamespace;
|
||||
|
||||
public sealed partial class MainWindow : WindowEx
|
||||
{
|
||||
public Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
|
||||
private UISettings _settings;
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
@ -11,5 +13,21 @@ public sealed partial class MainWindow : WindowEx
|
|||
AppWindow.SetIcon(Path.Combine(AppContext.BaseDirectory, "Assets/WindowIcon.ico"));
|
||||
Content = null;
|
||||
Title = "AppDisplayName".GetLocalized();
|
||||
|
||||
// Theme change code picked from https://github.com/microsoft/WinUI-Gallery/pull/1239
|
||||
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
|
||||
_settings = new UISettings();
|
||||
_settings.ColorValuesChanged += _settings_ColorValuesChanged; // cannot use FrameworkElement.ActualThemeChanged event because the triggerTitleBarRepaint workaround no longer works
|
||||
}
|
||||
|
||||
// this handles updating the caption button colors correctly when indows system theme is changed
|
||||
// while the app is open
|
||||
private void _settings_ColorValuesChanged(UISettings sender, object args)
|
||||
{
|
||||
// This calls comes off-thread, hence we will need to dispatch it to current app's thread
|
||||
dispatcherQueue.TryEnqueue(() =>
|
||||
{
|
||||
TitleBarHelper.ApplySystemThemeToCaptionButtons();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ public sealed partial class ShellPage : Page
|
|||
var resource = args.WindowActivationState == WindowActivationState.Deactivated ? "WindowCaptionForegroundDisabled" : "WindowCaptionForeground";
|
||||
|
||||
AppTitleBarText.Foreground = (SolidColorBrush)App.Current.Resources[resource];
|
||||
App.AppTitlebar = AppTitleBarText as UIElement;
|
||||
}
|
||||
|
||||
private void OnUnloaded(object sender, RoutedEventArgs e)
|
||||
|
|
|
@ -47,6 +47,8 @@ public sealed partial class ShellPage : Page
|
|||
var resource = args.WindowActivationState == WindowActivationState.Deactivated ? "WindowCaptionForegroundDisabled" : "WindowCaptionForeground";
|
||||
|
||||
AppTitleBarText.Foreground = (SolidColorBrush)App.Current.Resources[resource];
|
||||
App.AppTitlebar = AppTitleBarText as UIElement;
|
||||
|
||||
}
|
||||
|
||||
private void NavigationViewControl_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)
|
||||
|
|
Загрузка…
Ссылка в новой задаче