在Xamarin Forms Custom Renderer中将Mapbox添加到Android视图

时间:2016-01-29 21:04:35

标签: android xamarin xamarin.forms mapbox

我无法弄清楚如何使用Xamarin.Forms在Android上的自定义视图渲染器中获取Mapbox地图。它让我疯狂。

在我的PCL中,我有一张地图视图。

public class MapView: View
{
    public MapView() { }
}

对于iOS,"入门"帮助足够接近让它在iOS上运行,如下所示:

[assembly: ExportRenderer (typeof(Shared.Mobile.MapView), typeof(MapViewRenderer))]
namespace Clients.iOS
{
    public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, UIView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Shared.Mobile.MapView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement == null)
            {
                return;
            }

            var uiView = new UIView(new CoreGraphics.CGRect(0, 0, 500, 700));
            SetNativeControl(uiView);

            var mapView = new MapView(Control.Bounds);
            mapView.SetCenterCoordinate(new CoreLocation.CLLocationCoordinate2D(40.81, -96.68), false);
            mapView.SetZoomLevel(11, false);

            mapView.AddAnnotation(new PointAnnotation
                {
                    Coordinate = new CoreLocation.CLLocationCoordinate2D(40.81, -96.68), 
                    Title = "Lincoln, NE",
                    Subtitle = "What-what"
                });

            uiView.AddSubview(mapView);
        }
    }
}

在Android方面,没有那么多。 (https://components.xamarin.com/gettingstarted/mapboxsdk)。他们正在使用XML和各种各样的活动,但我在移动设备方面的知识目前并不远离Xamarin.Forms,所以我似乎无法弥合两者之间的差距。我的Android渲染器如下所示:

public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, Android.Views.View>
{     
    protected override void OnElementChanged(ElementChangedEventArgs<Shared.Mobile.MapView> e)
    {
        base.OnElementChanged(e);

        var view = new Android.Views.View(Context);
        SetNativeControl(view);  // NullReferenceException will be thrown if the native control is not set

        if (Control == null)
        {
            return;
        }

        var mapView = new MapView(Forms.Context, "thisismyaccesscode");
        mapView.CenterCoordinate = new LatLng(41.885, -87.679);
        mapView.ZoomLevel = 11;
        mapView.SetMinimumHeight(250);
        mapView.SetMinimumWidth(250);
        mapView.AddMarker(new MarkerOptions().SetPosition(new LatLng(40.81, -96.68)).SetTitle("Lincoln, NE"));

        view.AddSubview(mapView) // I wish this method existed
    }
}

我对AddSubview(mapView)的最后调用实际上并不是View类的方法,因为它是iOS上的UIView类。这就是我被困的地方。我无法弄清楚如何显示MapView。请帮忙。

1 个答案:

答案 0 :(得分:1)

正如您已经提到的那样,您无法拨打AddSubview,因为它是iOS方法。

Android上,它相当于AddView

但是 - 您试图在Android View对象上而不是ViewGroup对象上执行此类操作,因此无法进行此操作。

首先而不是: -

var view = new Android.Views.View(Context);

尝试直接实例化MapView,例如: -

var view = new MapView(Context, "thisismyaccesscode");

您对该视图的SetNativeControl来电没问题。

我没有尝试过Mapbox组件,所以我不清楚它在上面的代码中所期望的确切参数类型。

如果这不起作用,那么请执行以下操作: -

var view = new Android.Widget.FrameLayout(Context);

var mapView = new MapView(Forms.Context, "thisismyaccesscode");
mapView.CenterCoordinate = new LatLng(41.885, -87.679);
mapView.ZoomLevel = 11;
mapView.SetMinimumHeight(250);
mapView.SetMinimumWidth(250);
mapView.AddMarker(new MarkerOptions().SetPosition(new LatLng(40.81, -96.68)).SetTitle("Lincoln, NE"));

view.AddView(mapView);

SetNativeControl(view);

您还必须将第一行更改为以下内容: -

public class MapViewRenderer : ViewRenderer<Shared.Mobile.MapView, Android.Widget.FrameLayout>