Skip to content

Commit 33bda60

Browse files
Fix for #2407 - MapHandler Throws InvalidSystemOperationExcetption (#2409)
* Call EnsureCoreWebView2Async() before trying to execute script Fixes #2407 * Rename to `TryCallJSMethod` * Update MapHandler.Windows.cs --------- Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
1 parent 1f67ed4 commit 33bda60

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<ApplicationVersion>1</ApplicationVersion>
2323

2424
<!--
25-
Uncomment the line below if you need to debug the SG code
25+
Uncomment the lines below if you need to debug the SG code
2626
If you see any LongPath issue on Windows, check this doc
2727
https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later
2828
-->

src/CommunityToolkit.Maui.Maps/Handler/Map/MapHandler.Windows.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected override FrameworkElement CreatePlatformView()
6262
webView.NavigationCompleted += HandleWebViewNavigationCompleted;
6363
webView.WebMessageReceived += WebViewWebMessageReceived;
6464
webView.LoadHtml(mapPage, null);
65+
6566
return webView;
6667
}
6768

@@ -88,31 +89,31 @@ protected override void DisconnectHandler(FrameworkElement platformView)
8889
/// </summary>
8990
public static new Task MapMapType(IMapHandler handler, IMap map)
9091
{
91-
return CallJSMethod(handler.PlatformView, $"setMapType('{map.MapType}');");
92+
return TryCallJSMethod(handler.PlatformView, $"setMapType('{map.MapType}');");
9293
}
9394

9495
/// <summary>
9596
/// Maps IsZoomEnabled
9697
/// </summary>
9798
public static new Task MapIsZoomEnabled(IMapHandler handler, IMap map)
9899
{
99-
return CallJSMethod(handler.PlatformView, $"disableMapZoom({(!map.IsZoomEnabled).ToString().ToLower()});");
100+
return TryCallJSMethod(handler.PlatformView, $"disableMapZoom({(!map.IsZoomEnabled).ToString().ToLower()});");
100101
}
101102

102103
/// <summary>
103104
/// Maps IsScrollEnabled
104105
/// </summary>
105106
public static new Task MapIsScrollEnabled(IMapHandler handler, IMap map)
106107
{
107-
return CallJSMethod(handler.PlatformView, $"disablePanning({(!map.IsScrollEnabled).ToString().ToLower()});");
108+
return TryCallJSMethod(handler.PlatformView, $"disablePanning({(!map.IsScrollEnabled).ToString().ToLower()});");
108109
}
109110

110111
/// <summary>
111112
/// Maps IsTrafficEnabled
112113
/// </summary>
113114
public static new Task MapIsTrafficEnabled(IMapHandler handler, IMap map)
114115
{
115-
return CallJSMethod(handler.PlatformView, $"disableTraffic({(!map.IsTrafficEnabled).ToString().ToLower()});");
116+
return TryCallJSMethod(handler.PlatformView, $"disableTraffic({(!map.IsTrafficEnabled).ToString().ToLower()});");
116117
}
117118

118119
/// <summary>
@@ -123,14 +124,14 @@ protected override void DisconnectHandler(FrameworkElement platformView)
123124
if (map.IsShowingUser)
124125
{
125126
var location = await GetCurrentLocation();
126-
if (location != null)
127+
if (location is not null)
127128
{
128-
await CallJSMethod(handler.PlatformView, $"addLocationPin({location.Latitude.ToString(CultureInfo.InvariantCulture)},{location.Longitude.ToString(CultureInfo.InvariantCulture)});");
129+
await TryCallJSMethod(handler.PlatformView, $"addLocationPin({location.Latitude.ToString(CultureInfo.InvariantCulture)},{location.Longitude.ToString(CultureInfo.InvariantCulture)});");
129130
}
130131
}
131132
else
132133
{
133-
await CallJSMethod(handler.PlatformView, "removeLocationPin();");
134+
await TryCallJSMethod(handler.PlatformView, "removeLocationPin();");
134135
}
135136
}
136137

@@ -139,13 +140,13 @@ protected override void DisconnectHandler(FrameworkElement platformView)
139140
/// </summary>
140141
public static new async Task MapPins(IMapHandler handler, IMap map)
141142
{
142-
await CallJSMethod(handler.PlatformView, "removeAllPins();");
143+
await TryCallJSMethod(handler.PlatformView, "removeAllPins();");
143144

144145
var addPinTaskList = new List<Task>();
145146

146147
foreach (var pin in map.Pins)
147148
{
148-
addPinTaskList.Add(CallJSMethod(handler.PlatformView, $"addPin({pin.Location.Latitude.ToString(CultureInfo.InvariantCulture)}," +
149+
addPinTaskList.Add(TryCallJSMethod(handler.PlatformView, $"addPin({pin.Location.Latitude.ToString(CultureInfo.InvariantCulture)}," +
149150
$"{pin.Location.Longitude.ToString(CultureInfo.InvariantCulture)},'{pin.Label}', '{pin.Address}', '{(pin as Pin)?.Id}');"));
150151
}
151152

@@ -172,22 +173,33 @@ protected override void DisconnectHandler(FrameworkElement platformView)
172173
mapHandler.regionToGo = newRegion;
173174
}
174175

175-
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)});");
176+
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)});");
176177
}
177178

178-
static async Task CallJSMethod(FrameworkElement platformWebView, string script)
179+
static async Task<bool> TryCallJSMethod(FrameworkElement platformWebView, string script)
179180
{
180-
if (platformWebView is WebView2 webView2)
181+
if (platformWebView is not WebView2 webView2)
181182
{
182-
var tcs = new TaskCompletionSource();
183-
webView2.DispatcherQueue.TryEnqueue(async () =>
184-
{
185-
await webView2.ExecuteScriptAsync(script);
186-
tcs.SetResult();
187-
});
183+
return false;
184+
}
188185

189-
await tcs.Task;
186+
await webView2.EnsureCoreWebView2Async();
187+
188+
var tcs = new TaskCompletionSource();
189+
var isEnqueueSuccessful = webView2.DispatcherQueue.TryEnqueue(async () =>
190+
{
191+
await webView2.ExecuteScriptAsync(script);
192+
tcs.SetResult();
193+
});
194+
195+
if (!isEnqueueSuccessful)
196+
{
197+
return false;
190198
}
199+
200+
await tcs.Task;
201+
202+
return true;
191203
}
192204

193205
static string GetMapHtmlPage(string key)
@@ -413,7 +425,7 @@ async void HandleWebViewNavigationCompleted(WebView2 sender, CoreWebView2Navigat
413425
// Update initial properties when our page is loaded
414426
Mapper.UpdateProperties(this, VirtualView);
415427

416-
if (regionToGo != null)
428+
if (regionToGo is not null)
417429
{
418430
await MapMoveToRegion(this, VirtualView, regionToGo);
419431
}
@@ -476,7 +488,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
476488
var hideInfoWindow = clickedPin?.SendInfoWindowClick();
477489
if (hideInfoWindow is not false)
478490
{
479-
await CallJSMethod(PlatformView, "hideInfoWindow();");
491+
await TryCallJSMethod(PlatformView, "hideInfoWindow();");
480492
}
481493
}
482494
break;
@@ -492,7 +504,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
492504
var hideInfoWindow = clickedPin?.SendMarkerClick();
493505
if (hideInfoWindow is not false)
494506
{
495-
await CallJSMethod(PlatformView, "hideInfoWindow();");
507+
await TryCallJSMethod(PlatformView, "hideInfoWindow();");
496508
}
497509
}
498510
break;

src/CommunityToolkit.Maui/CommunityToolkit.Maui.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<IsAotCompatible>true</IsAotCompatible>
1010

11-
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
12-
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
11+
<!--
12+
Uncomment the lines below if you need to debug the SG code
13+
If you see any LongPath issue on Windows, check this doc
14+
https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later
15+
-->
16+
<!--<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
17+
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath> -->
1318

1419
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1520

0 commit comments

Comments
 (0)