From 7cef3ac93f72520dbce6515013c06b7d52d6cb3b Mon Sep 17 00:00:00 2001 From: mikelor Date: Fri, 20 Dec 2024 16:16:05 -0800 Subject: [PATCH 1/2] Call EnsureCoreWebView2Async() before trying to execute script Fixes #2407 --- .../Handler/Map/MapHandler.Windows.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs b/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs index 04da317f1c..345766137d 100644 --- a/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs +++ b/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs @@ -179,6 +179,8 @@ static async Task CallJSMethod(FrameworkElement platformWebView, string script) { if (platformWebView is WebView2 webView2) { + await webView2.EnsureCoreWebView2Async(); + var tcs = new TaskCompletionSource(); webView2.DispatcherQueue.TryEnqueue(async () => { From f91b5491c0449956dda17b1a5ba9050d74be5132 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Tue, 24 Dec 2024 11:32:20 -0800 Subject: [PATCH 2/2] Rename to `TryCallJSMethod` --- .../CommunityToolkit.Maui.Sample.csproj | 2 +- .../Handler/Map/MapHandler.Windows.cs | 54 +++++++++++-------- .../CommunityToolkit.Maui.csproj | 9 +++- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj index 02e75fa0bc..014bef1750 100644 --- a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj +++ b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj @@ -22,7 +22,7 @@ 1 diff --git a/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs b/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs index 345766137d..6f84f917db 100644 --- a/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs +++ b/src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs @@ -62,6 +62,7 @@ protected override FrameworkElement CreatePlatformView() webView.NavigationCompleted += HandleWebViewNavigationCompleted; webView.WebMessageReceived += WebViewWebMessageReceived; webView.LoadHtml(mapPage, null); + return webView; } @@ -88,7 +89,7 @@ protected override void DisconnectHandler(FrameworkElement platformView) /// public static new Task MapMapType(IMapHandler handler, IMap map) { - return CallJSMethod(handler.PlatformView, $"setMapType('{map.MapType}');"); + return TryCallJSMethod(handler.PlatformView, $"setMapType('{map.MapType}');"); } /// @@ -96,7 +97,7 @@ protected override void DisconnectHandler(FrameworkElement platformView) /// public static new Task MapIsZoomEnabled(IMapHandler handler, IMap map) { - return CallJSMethod(handler.PlatformView, $"disableMapZoom({(!map.IsZoomEnabled).ToString().ToLower()});"); + return TryCallJSMethod(handler.PlatformView, $"disableMapZoom({(!map.IsZoomEnabled).ToString().ToLower()});"); } /// @@ -104,7 +105,7 @@ protected override void DisconnectHandler(FrameworkElement platformView) /// public static new Task MapIsScrollEnabled(IMapHandler handler, IMap map) { - return CallJSMethod(handler.PlatformView, $"disablePanning({(!map.IsScrollEnabled).ToString().ToLower()});"); + return TryCallJSMethod(handler.PlatformView, $"disablePanning({(!map.IsScrollEnabled).ToString().ToLower()});"); } /// @@ -112,7 +113,7 @@ protected override void DisconnectHandler(FrameworkElement platformView) /// public static new Task MapIsTrafficEnabled(IMapHandler handler, IMap map) { - return CallJSMethod(handler.PlatformView, $"disableTraffic({(!map.IsTrafficEnabled).ToString().ToLower()});"); + return TryCallJSMethod(handler.PlatformView, $"disableTraffic({(!map.IsTrafficEnabled).ToString().ToLower()});"); } /// @@ -123,14 +124,14 @@ protected override void DisconnectHandler(FrameworkElement platformView) if (map.IsShowingUser) { var location = await GetCurrentLocation(); - if (location != null) + if (location is not null) { - await CallJSMethod(handler.PlatformView, $"addLocationPin({location.Latitude.ToString(CultureInfo.InvariantCulture)},{location.Longitude.ToString(CultureInfo.InvariantCulture)});"); + await TryCallJSMethod(handler.PlatformView, $"addLocationPin({location.Latitude.ToString(CultureInfo.InvariantCulture)},{location.Longitude.ToString(CultureInfo.InvariantCulture)});"); } } else { - await CallJSMethod(handler.PlatformView, "removeLocationPin();"); + await TryCallJSMethod(handler.PlatformView, "removeLocationPin();"); } } @@ -139,13 +140,13 @@ protected override void DisconnectHandler(FrameworkElement platformView) /// public static new async Task MapPins(IMapHandler handler, IMap map) { - await CallJSMethod(handler.PlatformView, "removeAllPins();"); + await TryCallJSMethod(handler.PlatformView, "removeAllPins();"); var addPinTaskList = new List(); foreach (var pin in map.Pins) { - addPinTaskList.Add(CallJSMethod(handler.PlatformView, $"addPin({pin.Location.Latitude.ToString(CultureInfo.InvariantCulture)}," + + addPinTaskList.Add(TryCallJSMethod(handler.PlatformView, $"addPin({pin.Location.Latitude.ToString(CultureInfo.InvariantCulture)}," + $"{pin.Location.Longitude.ToString(CultureInfo.InvariantCulture)},'{pin.Label}', '{pin.Address}', '{(pin as Pin)?.Id}');")); } @@ -172,24 +173,31 @@ protected override void DisconnectHandler(FrameworkElement platformView) mapHandler.regionToGo = newRegion; } - await CallJSMethod(handler.PlatformView, $"setRegion({newRegion.Center.Latitude.ToString(CultureInfo.InvariantCulture)},{newRegion.Center.Longitude.ToString(CultureInfo.InvariantCulture)},{newRegion.LatitudeDegrees.ToString(CultureInfo.InvariantCulture)},{newRegion.LongitudeDegrees.ToString(CultureInfo.InvariantCulture)});"); + await TryCallJSMethod(handler.PlatformView, $"setRegion({newRegion.Center.Latitude.ToString(CultureInfo.InvariantCulture)},{newRegion.Center.Longitude.ToString(CultureInfo.InvariantCulture)},{newRegion.LatitudeDegrees.ToString(CultureInfo.InvariantCulture)},{newRegion.LongitudeDegrees.ToString(CultureInfo.InvariantCulture)});"); } - static async Task CallJSMethod(FrameworkElement platformWebView, string script) + static async Task TryCallJSMethod(FrameworkElement platformWebView, string script) { - if (platformWebView is WebView2 webView2) + if (platformWebView is not WebView2 webView2) { - await webView2.EnsureCoreWebView2Async(); + return false; + } - var tcs = new TaskCompletionSource(); - webView2.DispatcherQueue.TryEnqueue(async () => - { - await webView2.ExecuteScriptAsync(script); - tcs.SetResult(); - }); + var tcs = new TaskCompletionSource(); + var isEnqueueSuccessful = webView2.DispatcherQueue.TryEnqueue(async () => + { + await webView2.ExecuteScriptAsync(script); + tcs.SetResult(); + }); - await tcs.Task; + if (!isEnqueueSuccessful) + { + return false; } + + await tcs.Task; + + return true; } static string GetMapHtmlPage(string key) @@ -415,7 +423,7 @@ async void HandleWebViewNavigationCompleted(WebView2 sender, CoreWebView2Navigat // Update initial properties when our page is loaded Mapper.UpdateProperties(this, VirtualView); - if (regionToGo != null) + if (regionToGo is not null) { await MapMoveToRegion(this, VirtualView, regionToGo); } @@ -478,7 +486,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece var hideInfoWindow = clickedPin?.SendInfoWindowClick(); if (hideInfoWindow is not false) { - await CallJSMethod(PlatformView, "hideInfoWindow();"); + await TryCallJSMethod(PlatformView, "hideInfoWindow();"); } } break; @@ -494,7 +502,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece var hideInfoWindow = clickedPin?.SendMarkerClick(); if (hideInfoWindow is not false) { - await CallJSMethod(PlatformView, "hideInfoWindow();"); + await TryCallJSMethod(PlatformView, "hideInfoWindow();"); } } break; diff --git a/src/CommunityToolkit.Maui/CommunityToolkit.Maui.csproj b/src/CommunityToolkit.Maui/CommunityToolkit.Maui.csproj index 1b7881f10a..4ad219cbda 100644 --- a/src/CommunityToolkit.Maui/CommunityToolkit.Maui.csproj +++ b/src/CommunityToolkit.Maui/CommunityToolkit.Maui.csproj @@ -8,8 +8,13 @@ true true - true - $(BaseIntermediateOutputPath)\GeneratedFiles + + true