diff --git a/src/Core/src/Platform/Windows/MauiWebView.cs b/src/Core/src/Platform/Windows/MauiWebView.cs index ecca4879a9a5..99b4cdf52d6d 100644 --- a/src/Core/src/Platform/Windows/MauiWebView.cs +++ b/src/Core/src/Platform/Windows/MauiWebView.cs @@ -11,27 +11,20 @@ public class MauiWebView : WebView2, IWebViewDelegate { readonly WeakReference _handler; + [Obsolete("Constructor is no longer used, please use an overloaded version.")] +#pragma warning disable CS8618 + public MauiWebView() + { + SetupPlatformEvents(); + } +#pragma warning restore CS8618 + public MauiWebView(WebViewHandler handler) { ArgumentNullException.ThrowIfNull(handler, nameof(handler)); _handler = new WeakReference(handler); - NavigationStarting += (sender, args) => - { - // Auto map local virtual app dir host, e.g. if navigating back to local site from a link to an external site - if (args?.Uri?.ToLowerInvariant().StartsWith(LocalScheme.TrimEnd('/').ToLowerInvariant()) == true) - { - CoreWebView2.SetVirtualHostNameToFolderMapping( - LocalHostName, - ApplicationPath, - Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); - } - // Auto unmap local virtual app dir host if navigating to any other potentially unsafe domain - else - { - CoreWebView2.ClearVirtualHostNameToFolderMapping(LocalHostName); - } - }; + SetupPlatformEvents(); } WebView2? _internalWebView; @@ -132,7 +125,7 @@ public async void LoadUrl(string? url) uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute); } - if (_handler.TryGetTarget(out var handler)) + if (_handler?.TryGetTarget(out var handler) ?? false) await handler.SyncPlatformCookies(uri.AbsoluteUri); try @@ -144,5 +137,25 @@ public async void LoadUrl(string? url) Debug.WriteLine(nameof(MauiWebView), $"Failed to load: {uri} {exc}"); } } + + void SetupPlatformEvents() + { + NavigationStarting += (sender, args) => + { + // Auto map local virtual app dir host, e.g. if navigating back to local site from a link to an external site + if (args?.Uri?.ToLowerInvariant().StartsWith(LocalScheme.TrimEnd('/').ToLowerInvariant()) == true) + { + CoreWebView2.SetVirtualHostNameToFolderMapping( + LocalHostName, + ApplicationPath, + Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); + } + // Auto unmap local virtual app dir host if navigating to any other potentially unsafe domain + else + { + CoreWebView2.ClearVirtualHostNameToFolderMapping(LocalHostName); + } + }; + } } } diff --git a/src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt index cfcb47a2871a..33d52006c240 100644 --- a/src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt +++ b/src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt @@ -43,5 +43,4 @@ static Microsoft.Maui.PropertyMapperExtensions.ModifyMapping(this Microsoft.Maui.IPropertyMapper! propertyMapper, string! key, System.Action! method) -> void static Microsoft.Maui.PropertyMapperExtensions.ReplaceMapping(this Microsoft.Maui.IPropertyMapper! propertyMapper, string! key, System.Action! method) -> void static Microsoft.Maui.SizeRequest.operator !=(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool -static Microsoft.Maui.SizeRequest.operator ==(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool -*REMOVED*Microsoft.Maui.Platform.MauiWebView.MauiWebView() -> void \ No newline at end of file +static Microsoft.Maui.SizeRequest.operator ==(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool \ No newline at end of file