From 3e0da70ddfa70ef6ab7ceabfd6897ec8195feff5 Mon Sep 17 00:00:00 2001 From: wherewhere Date: Tue, 7 Nov 2023 16:17:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CoreWindow=20=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreAppUWP/App.xaml.cs | 3 +- CoreAppUWP/Controls/DesktopWindow.cs | 6 +- CoreAppUWP/Helpers/BackdropHelper.cs | 4 +- CoreAppUWP/Helpers/ThemeHelper.cs | 59 ++++++++++--------- .../Pages/SettingsPages/SettingsPage.xaml | 3 +- .../Pages/SettingsPages/SettingsPage.xaml.cs | 6 +- 6 files changed, 44 insertions(+), 37 deletions(-) diff --git a/CoreAppUWP/App.xaml.cs b/CoreAppUWP/App.xaml.cs index 6dff192..d69b398 100644 --- a/CoreAppUWP/App.xaml.cs +++ b/CoreAppUWP/App.xaml.cs @@ -3,6 +3,7 @@ using CoreAppUWP.Pages; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media.Animation; using Microsoft.UI.Xaml.Navigation; using System; using Windows.ApplicationModel.Core; @@ -90,7 +91,7 @@ private void EnsureWindow(LaunchActivatedEventArgs e) // 当导航堆栈尚未还原时,导航到第一页, // 并通过将所需信息作为导航参数传入来配置 // 参数 - rootFrame.Navigate(typeof(MainPage), e); + rootFrame.Navigate(typeof(MainPage), e, new DrillInNavigationTransitionInfo()); BackdropHelper.SetBackdrop(window, SettingsHelper.Get(SettingsHelper.SelectedBackdrop)); } diff --git a/CoreAppUWP/Controls/DesktopWindow.cs b/CoreAppUWP/Controls/DesktopWindow.cs index 03ac5a9..d282e0e 100644 --- a/CoreAppUWP/Controls/DesktopWindow.cs +++ b/CoreAppUWP/Controls/DesktopWindow.cs @@ -189,7 +189,11 @@ public static Task CreateAsync(DispatcherQueue dispatcherQueue, A /// /// Attempts to activate the application window by bringing it to the foreground and setting the input focus to it. /// - public void Activate() => AppWindow.Show(); + public void Activate() + { + AppWindow.Show(); + AppWindow.MoveInZOrderAtTop(); + } /// /// Closes the application window. diff --git a/CoreAppUWP/Helpers/BackdropHelper.cs b/CoreAppUWP/Helpers/BackdropHelper.cs index 31426b6..1a1705e 100644 --- a/CoreAppUWP/Helpers/BackdropHelper.cs +++ b/CoreAppUWP/Helpers/BackdropHelper.cs @@ -308,9 +308,9 @@ public static void SetAllBackdrop(BackdropType type) { ActiveWindows.Values.ForEach(async x => { - if (x.window.Dispatcher?.HasThreadAccess == false) + if (x.window.DispatcherQueue?.HasThreadAccess == false) { - await x.window.Dispatcher.ResumeForegroundAsync(); + await x.window.DispatcherQueue.ResumeForegroundAsync(); } x.SetBackdrop(type); }); diff --git a/CoreAppUWP/Helpers/ThemeHelper.cs b/CoreAppUWP/Helpers/ThemeHelper.cs index a81b252..82087de 100644 --- a/CoreAppUWP/Helpers/ThemeHelper.cs +++ b/CoreAppUWP/Helpers/ThemeHelper.cs @@ -1,13 +1,14 @@ -using CoreAppUWP.Common; +using CommunityToolkit.WinUI; +using CoreAppUWP.Common; using CoreAppUWP.Controls; using Microsoft.UI; +using Microsoft.UI.Dispatching; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using System.Linq; using System.Threading.Tasks; using Windows.ApplicationModel.Core; using Windows.UI; -using Windows.UI.Core; using Windows.UI.ViewManagement; namespace CoreAppUWP.Helpers @@ -20,7 +21,8 @@ public static class ThemeHelper private static Window CurrentApplicationWindow; // Keep reference so it does not get optimized/garbage collected - public static UISettings UISettings { get; private set; } + public static UISettings UISettings { get; } = new UISettings(); + public static AccessibilitySettings AccessibilitySettings { get; } = new AccessibilitySettings(); public static WeakEvent UISettingChanged { get; } = []; @@ -38,13 +40,13 @@ public static ElementTheme GetActualTheme() => public static ElementTheme GetActualTheme(Window window) => window == null ? SettingsHelper.Get(SettingsHelper.SelectedAppTheme) - : window.Dispatcher?.HasThreadAccess == false - ? window.Dispatcher?.AwaitableRunAsync(() => + : window.DispatcherQueue?.HasThreadAccess == false + ? window.DispatcherQueue?.EnqueueAsync(() => window.Content is FrameworkElement _rootElement && _rootElement.RequestedTheme != ElementTheme.Default ? _rootElement.RequestedTheme : SettingsHelper.Get(SettingsHelper.SelectedAppTheme), - CoreDispatcherPriority.High)?.AwaitByTaskCompleteSource() + DispatcherQueuePriority.High)?.AwaitByTaskCompleteSource() ?? SettingsHelper.Get(SettingsHelper.SelectedAppTheme) : window.Content is FrameworkElement rootElement && rootElement.RequestedTheme != ElementTheme.Default @@ -57,13 +59,13 @@ public static Task GetActualThemeAsync() => public static async Task GetActualThemeAsync(Window window) => window == null ? SettingsHelper.Get(SettingsHelper.SelectedAppTheme) - : window.Dispatcher?.HasThreadAccess == false - ? await window.Dispatcher?.AwaitableRunAsync(() => + : window.DispatcherQueue?.HasThreadAccess == false + ? await window.DispatcherQueue?.EnqueueAsync(() => window.Content is FrameworkElement _rootElement && _rootElement.RequestedTheme != ElementTheme.Default ? _rootElement.RequestedTheme : SettingsHelper.Get(SettingsHelper.SelectedAppTheme), - CoreDispatcherPriority.High) + DispatcherQueuePriority.High) : window.Content is FrameworkElement rootElement && rootElement.RequestedTheme != ElementTheme.Default ? rootElement.RequestedTheme @@ -88,15 +90,15 @@ public static ElementTheme GetRootTheme() => public static ElementTheme GetRootTheme(Window window) => window == null ? ElementTheme.Default - : window.Dispatcher.HasThreadAccess + : window.DispatcherQueue.HasThreadAccess ? window.Content is FrameworkElement rootElement ? rootElement.RequestedTheme : ElementTheme.Default - : window.Dispatcher.AwaitableRunAsync(() => + : window.DispatcherQueue.EnqueueAsync(() => window.Content is FrameworkElement _rootElement ? _rootElement.RequestedTheme : ElementTheme.Default, - CoreDispatcherPriority.High).AwaitByTaskCompleteSource(); + DispatcherQueuePriority.High).AwaitByTaskCompleteSource(); public static Task GetRootThemeAsync() => GetRootThemeAsync(Window.Current ?? CurrentApplicationWindow); @@ -104,23 +106,23 @@ public static Task GetRootThemeAsync() => public static async Task GetRootThemeAsync(Window window) => window == null ? ElementTheme.Default - : window.Dispatcher.HasThreadAccess + : window.DispatcherQueue.HasThreadAccess ? window.Content is FrameworkElement rootElement ? rootElement.RequestedTheme : ElementTheme.Default - : await window.Dispatcher.AwaitableRunAsync(() => + : await window.DispatcherQueue.EnqueueAsync(() => window.Content is FrameworkElement _rootElement ? _rootElement.RequestedTheme : ElementTheme.Default, - CoreDispatcherPriority.High); + DispatcherQueuePriority.High); public static async void SetRootTheme(ElementTheme value) { WindowHelper.ActiveWindows.Values.ForEach(async window => { - if (!window.Dispatcher.HasThreadAccess) + if (window.DispatcherQueue?.HasThreadAccess == false) { - await window.Dispatcher.ResumeForegroundAsync(); + await window.DispatcherQueue.ResumeForegroundAsync(); } if (window.Content is FrameworkElement rootElement) @@ -151,9 +153,9 @@ public static async Task SetRootThemeAsync(ElementTheme value) { await Task.WhenAll(WindowHelper.ActiveWindows.Values.Select(async window => { - if (!window.Dispatcher.HasThreadAccess) + if (window.DispatcherQueue?.HasThreadAccess == false) { - await window.Dispatcher.ResumeForegroundAsync(); + await window.DispatcherQueue.ResumeForegroundAsync(); } if (window.Content is FrameworkElement rootElement) @@ -189,7 +191,6 @@ public static void Initialize() RootTheme = SettingsHelper.Get(SettingsHelper.SelectedAppTheme); // Registering to color changes, thus we notice when user changes theme system wide - UISettings = new UISettings(); UISettings.ColorValuesChanged += UISettings_ColorValuesChanged; } @@ -256,9 +257,9 @@ public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar) { WindowHelper.ActiveWindows.Values.ForEach(async window => { - if (window.Dispatcher?.HasThreadAccess == false) + if (window.DispatcherQueue?.HasThreadAccess == false) { - await window.Dispatcher.ResumeForegroundAsync(); + await window.DispatcherQueue.ResumeForegroundAsync(); } CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = IsExtendsTitleBar; }); @@ -278,16 +279,16 @@ public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar) public static async void UpdateSystemCaptionButtonColors() { bool IsDark = await IsDarkThemeAsync(); - bool IsHighContrast = new AccessibilitySettings().HighContrast; + bool IsHighContrast = AccessibilitySettings.HighContrast; Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black; Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243); WindowHelper.ActiveWindows.Values.ForEach(async window => { - if (window.Dispatcher?.HasThreadAccess == false) + if (window.DispatcherQueue?.HasThreadAccess == false) { - await window.Dispatcher.ResumeForegroundAsync(); + await window.DispatcherQueue.ResumeForegroundAsync(); } bool ExtendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar; @@ -316,13 +317,13 @@ public static async void UpdateSystemCaptionButtonColors() public static async void UpdateSystemCaptionButtonColors(Window window) { - if (window.Dispatcher?.HasThreadAccess == false) + if (window.DispatcherQueue?.HasThreadAccess == false) { - await window.Dispatcher.ResumeForegroundAsync(); + await window.DispatcherQueue.ResumeForegroundAsync(); } bool IsDark = window?.Content is FrameworkElement rootElement ? rootElement.RequestedTheme.IsDarkTheme() : await IsDarkThemeAsync(); - bool IsHighContrast = new AccessibilitySettings().HighContrast; + bool IsHighContrast = AccessibilitySettings.HighContrast; Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black; Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243); @@ -345,7 +346,7 @@ public static async void UpdateSystemCaptionButtonColors(DesktopWindow window) } bool IsDark = window?.Content is FrameworkElement rootElement ? rootElement.RequestedTheme.IsDarkTheme() : await IsDarkThemeAsync(); - bool IsHighContrast = new AccessibilitySettings().HighContrast; + bool IsHighContrast = AccessibilitySettings.HighContrast; Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black; Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243); diff --git a/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml b/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml index 9f37960..7c4536f 100644 --- a/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml +++ b/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml @@ -119,7 +119,6 @@ Description="Create a new CoreWindow." Header="CoreWindow" IsClickEnabled="True" - Style="{StaticResource ClickableSettingExpanderItemStyle}" Tag="NewWindow" /> @@ -158,6 +158,7 @@ Description="Show search charm pane." Header="Open Search" IsClickEnabled="True" + IsEnabled="{x:Bind IsCoreWindow}" Tag="SearchFlyout"> diff --git a/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs b/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs index e69014c..ff5894f 100644 --- a/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs +++ b/CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs @@ -99,7 +99,8 @@ private async void Button_Click(object sender, RoutedEventArgs e) Frame _frame = new(); window.Content = _frame; ThemeHelper.Initialize(window); - _ = _frame.Navigate(typeof(MainPage)); + try { _ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo()); } + catch { _ = _frame.Navigate(typeof(MainPage)); } BackdropHelper.SetBackdrop(window, SettingsHelper.Get(SettingsHelper.SelectedBackdrop)); }); break; @@ -118,8 +119,7 @@ private async void Button_Click(object sender, RoutedEventArgs e) AppWindow appWindow = window.AppWindow; appWindow.Title = Package.Current.DisplayName; appWindow.SetIcon("favicon.ico"); - appWindow.Show(); - appWindow.MoveInZOrderAtTop(); + window.Activate(); break; case "SearchFlyout" when SettingsPaneRegister.IsSearchPaneSupported: SearchPane.GetForCurrentView().Show();