From 78f1e03e38eafa59f41d1a0c47b4d862aa022872 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Fri, 17 Oct 2025 11:33:04 +0000 Subject: [PATCH 1/2] Fix: Open external links in default browser instead of WebView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When users click links in Monaco editor content, they now open in the system's default browser instead of navigating within the WebView. The fix intercepts the NavigationStarting event and: - Detects if the URL is external (different host/scheme from app) - Cancels the internal navigation - Opens the URL using Process.Start with UseShellExecute This provides a better user experience and prevents the app from being stuck on external web pages. Fixes #62 Co-authored-by: 小小高 --- SharpPad.Desktop/Views/MainWindow.axaml.cs | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/SharpPad.Desktop/Views/MainWindow.axaml.cs b/SharpPad.Desktop/Views/MainWindow.axaml.cs index 2152ac8..d69a66a 100644 --- a/SharpPad.Desktop/Views/MainWindow.axaml.cs +++ b/SharpPad.Desktop/Views/MainWindow.axaml.cs @@ -19,6 +19,41 @@ public MainWindow() private void OnNavigationStarting(object? sender, WebViewUrlLoadingEventArg e) { + // 获取当前应用的基础URL + if (DataContext is MainWindowViewModel viewModel) + { + var appBaseUrl = viewModel.WebUrl; + var navigationUrl = e.Url?.ToString() ?? string.Empty; + + // 如果导航URL不是空的,并且不是应用的基础URL(或其子路径) + if (!string.IsNullOrEmpty(navigationUrl) && + !string.IsNullOrEmpty(appBaseUrl) && + Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var appUri) && + Uri.TryCreate(navigationUrl, UriKind.Absolute, out var navUri)) + { + // 如果是外部链接(不同的host或scheme) + if (navUri.Host != appUri.Host || navUri.Scheme != appUri.Scheme) + { + // 取消WebView导航 + e.Cancel = true; + + // 在系统默认浏览器中打开 + try + { + var psi = new System.Diagnostics.ProcessStartInfo + { + FileName = navigationUrl, + UseShellExecute = true + }; + System.Diagnostics.Process.Start(psi); + } + catch (Exception ex) + { + Console.WriteLine($"无法打开外部链接: {navigationUrl}, 错误: {ex.Message}"); + } + } + } + } } private void OnNavigationCompleted(object? sender, WebViewUrlLoadedEventArg e) From b9fd4dc0618c929a91d0d89df0f5c2c70126218f Mon Sep 17 00:00:00 2001 From: gaocong Date: Mon, 20 Oct 2025 11:43:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=BA=20WebView=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=B0=E7=AA=97=E5=8F=A3=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 MainWindow.axaml 中为 WebView 添加了 WebViewNewWindowRequested 事件处理器。 在 MainWindow.axaml.cs 中: - 引入了 WebViewCore.Enums 命名空间。 - 删除了 OnNavigationStarting 方法中处理外部链接导航的逻辑。 - 新增了 OnNewWindowRequested 方法,用于处理外部链接导航,支持在系统默认浏览器中打开外部链接。 --- SharpPad.Desktop/Views/MainWindow.axaml | 3 +- SharpPad.Desktop/Views/MainWindow.axaml.cs | 66 ++++++++++------------ 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/SharpPad.Desktop/Views/MainWindow.axaml b/SharpPad.Desktop/Views/MainWindow.axaml index c6a34d2..8ac3c09 100644 --- a/SharpPad.Desktop/Views/MainWindow.axaml +++ b/SharpPad.Desktop/Views/MainWindow.axaml @@ -31,7 +31,8 @@ Opacity="{Binding WebViewOpacity}" Background="#1E1E1E" NavigationStarting="OnNavigationStarting" - NavigationCompleted="OnNavigationCompleted"> + NavigationCompleted="OnNavigationCompleted" + WebViewNewWindowRequested="OnNewWindowRequested"> diff --git a/SharpPad.Desktop/Views/MainWindow.axaml.cs b/SharpPad.Desktop/Views/MainWindow.axaml.cs index d69a66a..1a78d53 100644 --- a/SharpPad.Desktop/Views/MainWindow.axaml.cs +++ b/SharpPad.Desktop/Views/MainWindow.axaml.cs @@ -4,6 +4,7 @@ using SharpPad.Desktop.ViewModels; using WebViewCore.Events; using System; +using WebViewCore.Enums; namespace SharpPad.Desktop.Views; @@ -19,41 +20,6 @@ public MainWindow() private void OnNavigationStarting(object? sender, WebViewUrlLoadingEventArg e) { - // 获取当前应用的基础URL - if (DataContext is MainWindowViewModel viewModel) - { - var appBaseUrl = viewModel.WebUrl; - var navigationUrl = e.Url?.ToString() ?? string.Empty; - - // 如果导航URL不是空的,并且不是应用的基础URL(或其子路径) - if (!string.IsNullOrEmpty(navigationUrl) && - !string.IsNullOrEmpty(appBaseUrl) && - Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var appUri) && - Uri.TryCreate(navigationUrl, UriKind.Absolute, out var navUri)) - { - // 如果是外部链接(不同的host或scheme) - if (navUri.Host != appUri.Host || navUri.Scheme != appUri.Scheme) - { - // 取消WebView导航 - e.Cancel = true; - - // 在系统默认浏览器中打开 - try - { - var psi = new System.Diagnostics.ProcessStartInfo - { - FileName = navigationUrl, - UseShellExecute = true - }; - System.Diagnostics.Process.Start(psi); - } - catch (Exception ex) - { - Console.WriteLine($"无法打开外部链接: {navigationUrl}, 错误: {ex.Message}"); - } - } - } - } } private void OnNavigationCompleted(object? sender, WebViewUrlLoadedEventArg e) @@ -81,4 +47,32 @@ protected override void OnClosed(EventArgs e) base.OnClosed(e); _bridge.Dispose(); } -} + + private void OnNewWindowRequested(object? sender, WebViewNewWindowEventArgs e) + { + // 获取当前应用的基础URL + if (DataContext is MainWindowViewModel viewModel) + { + var appBaseUrl = viewModel.WebUrl; + var navigationUrl = e.Url?.ToString() ?? string.Empty; + + // 如果导航URL不是空的,并且不是应用的基础URL(或其子路径) + if (!string.IsNullOrEmpty(navigationUrl) && + !string.IsNullOrEmpty(appBaseUrl) && + Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var appUri) && + Uri.TryCreate(navigationUrl, UriKind.Absolute, out var navUri)) + { + // 如果是外部链接(不同的host或scheme) + if (navUri.Host != appUri.Host || navUri.Scheme != appUri.Scheme) + { + // 取消WebView导航 + if (Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var tempAppUri)) + { + e.Url = tempAppUri; + e.UrlLoadingStrategy = UrlRequestStrategy.OpenExternally; + } + } + } + } + } +} \ No newline at end of file