1
- using System . Diagnostics . CodeAnalysis ;
2
- using System . Globalization ;
1
+ using System . Globalization ;
3
2
using System . Text . Json ;
4
3
using Microsoft . Maui . Controls . Maps ;
5
4
using Microsoft . Maui . Handlers ;
15
14
namespace CommunityToolkit . Maui . Maps . Handlers ;
16
15
17
16
/// <inheritdoc />
18
- [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
19
- [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
20
17
public partial class MapHandlerWindows : MapHandler
21
18
{
22
19
MapSpan ? regionToGo ;
23
20
24
- readonly JsonSerializerOptions jsonSerializerOptions = new ( )
25
- {
26
- PropertyNameCaseInsensitive = true
27
- } ;
28
-
29
21
/// <summary>
30
22
/// Initializes a new instance of the <see cref="MapHandlerWindows"/> class.
31
23
/// </summary>
@@ -42,41 +34,43 @@ public MapHandlerWindows() : base(Mapper, CommandMapper)
42
34
}
43
35
44
36
internal static string ? MapsKey { get ; set ; }
37
+ internal static string ? MapPage { get ; private set ; }
45
38
46
39
/// <inheritdoc/>
47
- [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
48
- [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
49
- #pragma warning disable IL2046 // 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.
50
- #pragma warning disable IL3051 // 'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.
51
40
protected override FrameworkElement CreatePlatformView ( )
52
- #pragma warning restore IL3051 // 'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.
53
- #pragma warning restore IL2046 // 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.
54
41
{
55
42
if ( string . IsNullOrEmpty ( MapsKey ) )
56
43
{
57
44
throw new InvalidOperationException ( "You need to specify a Bing Maps Key" ) ;
58
45
}
59
46
60
- var mapPage = GetMapHtmlPage ( MapsKey ) ;
47
+ MapPage = GetMapHtmlPage ( MapsKey ) ;
61
48
var webView = new MauiWebView ( new WebViewHandler ( ) ) ;
62
- webView . NavigationCompleted += HandleWebViewNavigationCompleted ;
63
- webView . WebMessageReceived += WebViewWebMessageReceived ;
64
- webView . LoadHtml ( mapPage , null ) ;
65
49
66
50
return webView ;
67
51
}
68
52
69
53
/// <inheritdoc />
70
- [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
71
- [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
72
- #pragma warning disable IL2046 // 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.
73
- #pragma warning disable IL3051 // 'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.
54
+ protected override void ConnectHandler ( FrameworkElement platformView )
55
+ {
56
+ if ( platformView is MauiWebView mauiWebView )
57
+ {
58
+ LoadMap ( platformView ) ;
59
+
60
+ mauiWebView . NavigationCompleted += HandleWebViewNavigationCompleted ;
61
+ mauiWebView . WebMessageReceived += WebViewWebMessageReceived ;
62
+ Connectivity . ConnectivityChanged += Connectivity_ConnectivityChanged ;
63
+ }
64
+
65
+ base . ConnectHandler ( platformView ) ;
66
+ }
67
+
68
+ /// <inheritdoc />
74
69
protected override void DisconnectHandler ( FrameworkElement platformView )
75
- #pragma warning restore IL3051 // 'RequiresDynamicCodeAttribute' annotations must match across all interface implementations or overrides.
76
- #pragma warning restore IL2046 // 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.
77
70
{
78
- if ( PlatformView is MauiWebView mauiWebView )
71
+ if ( platformView is MauiWebView mauiWebView )
79
72
{
73
+ Connectivity . ConnectivityChanged -= Connectivity_ConnectivityChanged ;
80
74
mauiWebView . NavigationCompleted -= HandleWebViewNavigationCompleted ;
81
75
mauiWebView . WebMessageReceived -= WebViewWebMessageReceived ;
82
76
}
@@ -415,9 +409,21 @@ function hideInfoWindow()
415
409
416
410
static async Task < Location ? > GetCurrentLocation ( )
417
411
{
418
- var geoLocator = new Geolocator ( ) ;
419
- var position = await geoLocator . GetGeopositionAsync ( ) ;
420
- return new Location ( position . Coordinate . Latitude , position . Coordinate . Longitude ) ;
412
+ if ( await Geolocator . RequestAccessAsync ( ) != GeolocationAccessStatus . Allowed )
413
+ {
414
+ return null ;
415
+ }
416
+
417
+ try
418
+ {
419
+ var geoLocator = new Geolocator ( ) ;
420
+ var position = await geoLocator . GetGeopositionAsync ( ) ;
421
+ return new Location ( position . Coordinate . Latitude , position . Coordinate . Longitude ) ;
422
+ }
423
+ catch
424
+ {
425
+ return null ;
426
+ }
421
427
}
422
428
423
429
async void HandleWebViewNavigationCompleted ( WebView2 sender , CoreWebView2NavigationCompletedEventArgs args )
@@ -431,8 +437,6 @@ async void HandleWebViewNavigationCompleted(WebView2 sender, CoreWebView2Navigat
431
437
}
432
438
}
433
439
434
- [ RequiresDynamicCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
435
- [ RequiresUnreferencedCode ( "Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" ) ]
436
440
async void WebViewWebMessageReceived ( WebView2 sender , CoreWebView2WebMessageReceivedEventArgs args )
437
441
{
438
442
// For some reason the web message is empty
@@ -441,7 +445,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
441
445
return ;
442
446
}
443
447
444
- var eventMessage = JsonSerializer . Deserialize < EventMessage > ( args . WebMessageAsJson , jsonSerializerOptions ) ;
448
+ var eventMessage = JsonSerializer . Deserialize < EventMessage > ( args . WebMessageAsJson , SerializerContext . Default . EventMessage ) ;
445
449
446
450
// The web message (or it's ID) could not be deserialized to something we recognize
447
451
if ( eventMessage is null || ! Enum . TryParse < EventIdentifier > ( eventMessage . Id , true , out var eventId ) )
@@ -460,25 +464,23 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
460
464
switch ( eventId )
461
465
{
462
466
case EventIdentifier . BoundsChanged :
463
- var mapRect = JsonSerializer . Deserialize < Bounds > ( payloadAsString , jsonSerializerOptions ) ;
467
+ var mapRect = JsonSerializer . Deserialize < Bounds > ( payloadAsString , SerializerContext . Default . Bounds ) ;
464
468
if ( mapRect ? . Center is not null )
465
469
{
466
470
VirtualView . VisibleRegion = new MapSpan ( new Location ( mapRect . Center . Latitude , mapRect . Center . Longitude ) ,
467
471
mapRect . Height , mapRect . Width ) ;
468
472
}
469
473
break ;
470
474
case EventIdentifier . MapClicked :
471
- var clickedLocation = JsonSerializer . Deserialize < Location > ( payloadAsString ,
472
- jsonSerializerOptions ) ;
475
+ var clickedLocation = JsonSerializer . Deserialize < Location > ( payloadAsString , SerializerContext . Default . Location ) ;
473
476
if ( clickedLocation is not null )
474
477
{
475
478
VirtualView . Clicked ( clickedLocation ) ;
476
479
}
477
480
break ;
478
481
479
482
case EventIdentifier . InfoWindowClicked :
480
- var clickedInfoWindowWebView = JsonSerializer . Deserialize < InfoWindow > ( payloadAsString ,
481
- jsonSerializerOptions ) ;
483
+ var clickedInfoWindowWebView = JsonSerializer . Deserialize < InfoWindow > ( payloadAsString , SerializerContext . Default . InfoWindow ) ;
482
484
var clickedInfoWindowWebViewId = clickedInfoWindowWebView ? . InfoWindowMarkerId ;
483
485
484
486
if ( ! string . IsNullOrEmpty ( clickedInfoWindowWebViewId ) )
@@ -494,7 +496,7 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
494
496
break ;
495
497
496
498
case EventIdentifier . PinClicked :
497
- var clickedPinWebView = JsonSerializer . Deserialize < Pin > ( payloadAsString , jsonSerializerOptions ) ;
499
+ var clickedPinWebView = JsonSerializer . Deserialize < Pin > ( payloadAsString , SerializerContext . Default . Pin ) ;
498
500
var clickedPinWebViewId = clickedPinWebView ? . MarkerId ? . ToString ( ) ;
499
501
500
502
if ( ! string . IsNullOrEmpty ( clickedPinWebViewId ) )
@@ -510,4 +512,17 @@ async void WebViewWebMessageReceived(WebView2 sender, CoreWebView2WebMessageRece
510
512
break ;
511
513
}
512
514
}
515
+
516
+ void Connectivity_ConnectivityChanged ( object ? sender , ConnectivityChangedEventArgs e )
517
+ {
518
+ LoadMap ( PlatformView ) ;
519
+ }
520
+
521
+ static void LoadMap ( FrameworkElement platformView )
522
+ {
523
+ if ( platformView is MauiWebView mauiWebView && Connectivity . NetworkAccess == NetworkAccess . Internet )
524
+ {
525
+ mauiWebView . LoadHtml ( MapPage , null ) ;
526
+ }
527
+ }
513
528
}
0 commit comments