1
1
2
+ using Android . Content ;
3
+ using Android . Views ;
2
4
using MapboxMaui . ViewAnnotations ;
5
+ using Microsoft . Maui . Platform ;
3
6
4
7
namespace MapboxMaui ;
5
8
6
9
partial class MapboxViewHandler : IViewAnnotationController
7
10
{
8
- public void AddViewAnnotation ( ViewAnnotationOptions options , DataTemplate dataTemplate = null )
11
+ public void AddViewAnnotation ( ViewAnnotationOptions options , ContentView contentView = default )
9
12
{
10
13
var mapView = mapboxFragment ? . MapView ;
11
14
12
15
if ( mapView == null ) return ;
13
16
14
- dataTemplate = dataTemplate ?? VirtualView . DefaultViewAnnotationTemplate ;
17
+ contentView = contentView ?? VirtualView . AnnotationView ;
15
18
16
- if ( dataTemplate == null )
19
+ if ( contentView == null )
17
20
{
18
21
throw new InvalidOperationException ( "DataTemplate must be provided eiher via this method parameter or via DefaultViewAnnotationTemplate" ) ;
19
22
}
20
23
21
- var xview = ( View ) dataTemplate . CreateContent ( ) ;
22
- xview . Parent = VirtualView as Element ;
23
- xview . BindingContext = options ;
24
- xview . HeightRequest = options . Height ?? xview . HeightRequest ;
25
- xview . WidthRequest = options . Width ?? xview . WidthRequest ;
24
+ contentView . Parent = VirtualView as Element ;
25
+ contentView . BindingContext = options ;
26
26
27
- var platformHandler = TemplateHelpers . GetHandler (
28
- xview ,
29
- VirtualView . Handler . MauiContext ) ;
30
- platformHandler . PlatformView . LayoutParameters = new Android . Views . ViewGroup . LayoutParams (
31
- ( int ) options . Width . Value . PointToPixel ( ) ,
32
- ( int ) options . Height . Value . PointToPixel ( )
33
- ) ;
27
+ var handler = contentView . ToHandler ( MauiContext ) ;
28
+
29
+ var viewGroup = new ContentViewGroup ( Context )
30
+ {
31
+ LayoutParameters = new ViewGroup . LayoutParams (
32
+ ViewGroup . LayoutParams . WrapContent ,
33
+ ViewGroup . LayoutParams . WrapContent ) ,
34
+ CrossPlatformLayout = handler . VirtualView as ICrossPlatformLayout ,
35
+ } ;
34
36
35
37
mapView . ViewAnnotationManager . AddViewAnnotation (
36
- platformHandler . PlatformView ,
38
+ viewGroup ,
37
39
options . ToPlatform ( ) ) ;
38
40
}
39
41
@@ -45,4 +47,26 @@ public void RemoveAllViewAnnotations()
45
47
46
48
mapView . ViewAnnotationManager . RemoveAllViewAnnotations ( ) ;
47
49
}
50
+
51
+ class ViewAnnotationView : ViewGroup
52
+ {
53
+ public ViewAnnotationView ( Context context )
54
+ : base ( context )
55
+ {
56
+
57
+ }
58
+
59
+ protected override void OnLayout ( bool changed , int l , int t , int r , int b )
60
+ {
61
+ if ( ChildCount == 0 ) return ;
62
+
63
+ var view = this . GetChildAt ( 0 ) ;
64
+ view . Layout ( l , t , r , b ) ;
65
+ }
66
+
67
+ protected override void OnMeasure ( int widthMeasureSpec , int heightMeasureSpec )
68
+ {
69
+ base . OnMeasure ( widthMeasureSpec , heightMeasureSpec ) ;
70
+ }
71
+ }
48
72
}
0 commit comments