@@ -62,6 +62,7 @@ protected override FrameworkElement CreatePlatformView()
62
62
webView . NavigationCompleted += HandleWebViewNavigationCompleted ;
63
63
webView . WebMessageReceived += WebViewWebMessageReceived ;
64
64
webView . LoadHtml ( mapPage , null ) ;
65
+
65
66
return webView ;
66
67
}
67
68
@@ -88,31 +89,31 @@ protected override void DisconnectHandler(FrameworkElement platformView)
88
89
/// </summary>
89
90
public static new Task MapMapType ( IMapHandler handler , IMap map )
90
91
{
91
- return CallJSMethod ( handler . PlatformView , $ "setMapType('{ map . MapType } ');") ;
92
+ return TryCallJSMethod ( handler . PlatformView , $ "setMapType('{ map . MapType } ');") ;
92
93
}
93
94
94
95
/// <summary>
95
96
/// Maps IsZoomEnabled
96
97
/// </summary>
97
98
public static new Task MapIsZoomEnabled ( IMapHandler handler , IMap map )
98
99
{
99
- return CallJSMethod ( handler . PlatformView , $ "disableMapZoom({ ( ! map . IsZoomEnabled ) . ToString ( ) . ToLower ( ) } );") ;
100
+ return TryCallJSMethod ( handler . PlatformView , $ "disableMapZoom({ ( ! map . IsZoomEnabled ) . ToString ( ) . ToLower ( ) } );") ;
100
101
}
101
102
102
103
/// <summary>
103
104
/// Maps IsScrollEnabled
104
105
/// </summary>
105
106
public static new Task MapIsScrollEnabled ( IMapHandler handler , IMap map )
106
107
{
107
- return CallJSMethod ( handler . PlatformView , $ "disablePanning({ ( ! map . IsScrollEnabled ) . ToString ( ) . ToLower ( ) } );") ;
108
+ return TryCallJSMethod ( handler . PlatformView , $ "disablePanning({ ( ! map . IsScrollEnabled ) . ToString ( ) . ToLower ( ) } );") ;
108
109
}
109
110
110
111
/// <summary>
111
112
/// Maps IsTrafficEnabled
112
113
/// </summary>
113
114
public static new Task MapIsTrafficEnabled ( IMapHandler handler , IMap map )
114
115
{
115
- return CallJSMethod ( handler . PlatformView , $ "disableTraffic({ ( ! map . IsTrafficEnabled ) . ToString ( ) . ToLower ( ) } );") ;
116
+ return TryCallJSMethod ( handler . PlatformView , $ "disableTraffic({ ( ! map . IsTrafficEnabled ) . ToString ( ) . ToLower ( ) } );") ;
116
117
}
117
118
118
119
/// <summary>
@@ -123,14 +124,14 @@ protected override void DisconnectHandler(FrameworkElement platformView)
123
124
if ( map . IsShowingUser )
124
125
{
125
126
var location = await GetCurrentLocation ( ) ;
126
- if ( location != null )
127
+ if ( location is not null )
127
128
{
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 ) } );") ;
129
130
}
130
131
}
131
132
else
132
133
{
133
- await CallJSMethod ( handler . PlatformView , "removeLocationPin();" ) ;
134
+ await TryCallJSMethod ( handler . PlatformView , "removeLocationPin();" ) ;
134
135
}
135
136
}
136
137
@@ -139,13 +140,13 @@ protected override void DisconnectHandler(FrameworkElement platformView)
139
140
/// </summary>
140
141
public static new async Task MapPins ( IMapHandler handler , IMap map )
141
142
{
142
- await CallJSMethod ( handler . PlatformView , "removeAllPins();" ) ;
143
+ await TryCallJSMethod ( handler . PlatformView , "removeAllPins();" ) ;
143
144
144
145
var addPinTaskList = new List < Task > ( ) ;
145
146
146
147
foreach ( var pin in map . Pins )
147
148
{
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 ) } ," +
149
150
$ "{ pin . Location . Longitude . ToString ( CultureInfo . InvariantCulture ) } ,'{ pin . Label } ', '{ pin . Address } ', '{ ( pin as Pin ) ? . Id } ');") ) ;
150
151
}
151
152
@@ -172,22 +173,33 @@ protected override void DisconnectHandler(FrameworkElement platformView)
172
173
mapHandler . regionToGo = newRegion ;
173
174
}
174
175
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 ) } );") ;
176
177
}
177
178
178
- static async Task CallJSMethod ( FrameworkElement platformWebView , string script )
179
+ static async Task < bool > TryCallJSMethod ( FrameworkElement platformWebView , string script )
179
180
{
180
- if ( platformWebView is WebView2 webView2 )
181
+ if ( platformWebView is not WebView2 webView2 )
181
182
{
182
- var tcs = new TaskCompletionSource ( ) ;
183
- webView2 . DispatcherQueue . TryEnqueue ( async ( ) =>
184
- {
185
- await webView2 . ExecuteScriptAsync ( script ) ;
186
- tcs . SetResult ( ) ;
187
- } ) ;
183
+ return false ;
184
+ }
188
185
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 ;
190
198
}
199
+
200
+ await tcs . Task ;
201
+
202
+ return true ;
191
203
}
192
204
193
205
static string GetMapHtmlPage ( string key )
@@ -413,7 +425,7 @@ async void HandleWebViewNavigationCompleted(WebView2 sender, CoreWebView2Navigat
413
425
// Update initial properties when our page is loaded
414
426
Mapper . UpdateProperties ( this , VirtualView ) ;
415
427
416
- if ( regionToGo != null )
428
+ if ( regionToGo is not null )
417
429
{
418
430
await MapMoveToRegion ( this , VirtualView , regionToGo ) ;
419
431
}
@@ -476,7 +488,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
476
488
var hideInfoWindow = clickedPin ? . SendInfoWindowClick ( ) ;
477
489
if ( hideInfoWindow is not false )
478
490
{
479
- await CallJSMethod ( PlatformView , "hideInfoWindow();" ) ;
491
+ await TryCallJSMethod ( PlatformView , "hideInfoWindow();" ) ;
480
492
}
481
493
}
482
494
break ;
@@ -492,7 +504,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
492
504
var hideInfoWindow = clickedPin ? . SendMarkerClick ( ) ;
493
505
if ( hideInfoWindow is not false )
494
506
{
495
- await CallJSMethod ( PlatformView , "hideInfoWindow();" ) ;
507
+ await TryCallJSMethod ( PlatformView , "hideInfoWindow();" ) ;
496
508
}
497
509
}
498
510
break ;
0 commit comments