From 21b26f14cc6aeea45e4d3ffb21aeec529927c752 Mon Sep 17 00:00:00 2001 From: DaZiYuan Date: Tue, 6 Feb 2024 23:48:18 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E6=8B=A6=E6=88=AA=E6=96=B0=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E9=83=BD=E7=94=A8ShellWindow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Client/UI/ShellWindow.xaml.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/giantapp-wallpaper-client/Client/UI/ShellWindow.xaml.cs b/src/giantapp-wallpaper-client/Client/UI/ShellWindow.xaml.cs index 2f6e39d1..f114c798 100644 --- a/src/giantapp-wallpaper-client/Client/UI/ShellWindow.xaml.cs +++ b/src/giantapp-wallpaper-client/Client/UI/ShellWindow.xaml.cs @@ -156,31 +156,36 @@ public static void SetTheme(string theme, string mode) public static async void ShowShell(string? url) { - _logger.Info($"ShowShell {url}"); Instance ??= new ShellWindow(); + await Instance.ShowUrl(url); + } + + public async Task ShowUrl(string? url) + { + _logger.Info($"ShowShell {url}"); bool ok = await Task.Run(CheckWebView2); if (!ok) { //没装webview2 - Instance.loading.Visibility = Visibility.Collapsed; - Instance.tips.Visibility = Visibility.Visible; + loading.Visibility = Visibility.Collapsed; + tips.Visibility = Visibility.Visible; LoopCheckWebView2(url); } else { - Instance.loading.Visibility = Visibility.Visible; + loading.Visibility = Visibility.Visible; } - if (Instance.WindowState == WindowState.Minimized) - Instance.WindowState = WindowState.Normal; + if (WindowState == WindowState.Minimized) + WindowState = WindowState.Normal; - Instance.Activate(); + Activate(); - Instance.webview2.Source = new Uri(url); - Instance.webview2.NavigationCompleted += NavigationCompleted; - Instance.Show(); + webview2.Source = new Uri(url); + webview2.NavigationCompleted += NavigationCompleted; + Show(); } public static void ApplyCustomFolderMapping(Dictionary mapping, Microsoft.Web.WebView2.Wpf.WebView2? webview2 = null) @@ -317,6 +322,8 @@ protected override void OnClosed(EventArgs e) SizeChanged -= ShellWindow_SizeChanged; StateChanged -= ShellWindow_StateChanged; webview2.CoreWebView2InitializationCompleted -= Webview2_CoreWebView2InitializationCompleted; + webview2.CoreWebView2.NavigationStarting -= CoreWebView2_NavigationStarting; + webview2.CoreWebView2.NewWindowRequested -= CoreWebView2_NewWindowRequested; //webview2.CoreWebView2.WebMessageReceived -= CoreWebView2_WebMessageReceived; //强制回收webview2 @@ -337,6 +344,7 @@ private void Webview2_CoreWebView2InitializationCompleted(object? sender, CoreWe webview2.CoreWebView2.AddHostObjectToScript("shell", new ShellApiObject()); webview2.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting; //webview2.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; + webview2.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested; if (!AllowDragFile) DisableDragFile(); @@ -352,6 +360,14 @@ private void Webview2_CoreWebView2InitializationCompleted(object? sender, CoreWe #endif } + private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e) + { + e.Handled = true; + + var window = new ShellWindow(); + _ = window.ShowUrl(e.Uri); + } + private void CoreWebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e) { var uri = new Uri(e.Uri);