Xamarin的Andr​​oid表格马prenderer [英] Xamarin Forms Android MapRenderer

查看:142
本文介绍了Xamarin的Andr​​oid表格马prenderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地创建一个视图解析器为Xamarin的iOS,现在我想实现Android的一个渲染器,这里是地图,我需要在Android中实现:

I created successfully a ViewRenderer for Xamarin iOS and now I'm trying to implement a Renderer for Android, here is the Map That I need to implement in Android:

public class MapViewModel : Map
    {
        public static readonly BindableProperty LocationsProperty = BindableProperty.Create<MapViewModel, List<Branch>>(x => x.Locations, new List<Branch>());
        public static readonly BindableProperty PinTappedCommandProperty = BindableProperty.Create<MapViewModel, Command>(x => x.PinTapped, null);

        public MapViewModel(List<Branch> locations)
        {
            Locations = locations;
            PinTapped = new Command(async (x) =>
                {
                    //await Navigation.PopModalAsync();
                    var info = new InformationView((Branch)x);
                    await Navigation.PushAsync(info);
                });
        }

        public List<Branch> Locations
        {
            get { return (List<Branch>)GetValue(LocationsProperty); }
            set { SetValue(LocationsProperty, value); }
        }

        public Command PinTapped 
        {
            get {return (Command) GetValue(PinTappedCommandProperty); }
            set {SetValue(PinTappedCommandProperty, value);}
        }
    }

随着我所做的针对iOS我这样做是为Android:

Following what I did for iOS I did this for Android:

[assembly: ExportRenderer (typeof (MapViewModel), typeof (MapViewAndroid))]
public class MapViewAndroid : ViewRenderer<MapViewModel, global::Android.Gms.Maps.MapView>
    {
        private global::Android.Gms.Maps.MapView NativeMap {get { return Control; } }

        public MapViewAndroid ()
        {
        }
        protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged (sender, e);
        }
        protected override void OnElementChanged (ElementChangedEventArgs<MapViewModel> e)
        {
            base.OnElementChanged(e);
            var map = e.NewElement;
            var cmd = map.PinTapped;
            SetNativeControl(NativeMap);
        }
}

控制为空,所以我不能 SetNativeControl 正常和应用程序输出我读 [艺术] JNI RegisterNativeMethods:尝试注册对于kimo.customersapp.android.MapViewAndroid 0本地方法 ...
什么我缺少这工作?

The Control is null, so I can't SetNativeControl properly and in Application Output I read : [art] JNI RegisterNativeMethods: attempt to register 0 native methods for kimo.customersapp.android.MapViewAndroid... What I'm missing for this to works?

推荐答案

谷歌地图可能需要一些时间进行初始化。你需要等待它完全加载之前,你可以使用它。另外,还要确保你钩到IOnMa preadyCallback,让你得到通知时,地图上就可以使用了。

Google maps can take some time to initialize. You need to wait for it to fully load before you can use it. Also make sure you hook into IOnMapReadyCallback so that you get notified when the map is ready to use.

private GoogleMap NativeMap { get;set;}
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged (sender, e);
    if (e.PropertyName.Equals ("VisibleRegion")) {
        var targetElement = (Android.Gms.Maps.MapView)Control;
        if (NativeMap == null) {
            targetElement.GetMapAsync (this);
        }
    }

}

这篇关于Xamarin的Andr​​oid表格马prenderer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆