@@ -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 ;
0 commit comments