Xamarin:从MapRenderer类导航到ContentPage

时间:2017-02-01 14:50:34

标签: xamarin cross-platform content-pages

我面临以下问题。

我正在开发Xamarin的跨平台项目,我正在尝试从MapInfoWindow打开一个ContentPage。 Map ContentPage位于Portable Project内部,在Droid Project中,我有一个以下类,我正在尝试打开ContenPage:

public class CustomMapRenderer: MapRenderer, GoogleMap.IInfoWindowAdapter, IOnMapReadyCallback
{
    GoogleMap map;
    List<CustomPin> customPins;
    bool isDrawn;

    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            map.InfoWindowClick -= OnInfoWindowClick;
        }

        if (e.NewElement != null)
        {
            var formsMap = (CustomMap)e.NewElement;
            customPins = formsMap.CustomPins;
            ((MapView)Control).GetMapAsync(this);
        }
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        map = googleMap;
        map.InfoWindowClick += OnInfoWindowClick;
        map.SetInfoWindowAdapter(this);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName.Equals("VisibleRegion") && !isDrawn)
        {
            map.Clear();
            if (customPins != null)
            {
                foreach (var pin in customPins)
                {
                    var marker = new MarkerOptions();
                    marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude));
                    marker.SetTitle(pin.Pin.Label);
                    marker.SetSnippet(pin.Pin.Address);
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));

                    map.AddMarker(marker);
                }
                isDrawn = true;
            }
        }
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        base.OnLayout(changed, l, t, r, b);

        if (changed)
        {
            isDrawn = false;
        }
    }

    void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
    {
        var customPin = GetCustomPin(e.Marker);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }
         //Here I want to open the content page



    }

    public Android.Views.View GetInfoContents(Marker marker)
    {
        var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
        if (inflater != null)
        {
            Android.Views.View view;

            var customPin = GetCustomPin(marker);
            if (customPin == null)
            {
                throw new Exception("Custom pin not found");
            }

            if (customPin.Id == "Xamarin")
            {
                view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
            }
            else
            {
                view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
            }

            var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
            var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);

            if (infoTitle != null)
            {
                infoTitle.Text = marker.Title;
            }
            if (infoSubtitle != null)
            {
                infoSubtitle.Text = marker.Snippet;
            }

            return view;
        }
        return null;
    }

    public Android.Views.View GetInfoWindow(Marker marker)
    {
        return null;
    }

    CustomPin GetCustomPin(Marker annotation)
    {
        var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
        foreach (var pin in customPins)
        {
            if (pin.Pin.Position == position)
            {
                return pin;
            }
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

所以我相信在你的CustomMap类中创建一个>>> a = np.arange(10).reshape(5, 2) >>> b = np.arange(10,15) >>> c = np.c_[a,b] >>> a array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) >>> b array([10, 11, 12, 13, 14]) >>> c array([[ 0, 1, 10], [ 2, 3, 11], [ 4, 5, 12], [ 6, 7, 13], [ 8, 9, 14]]) >>> np.column_stack((a,b)) array([[ 0, 1, 10], [ 2, 3, 11], [ 4, 5, 12], [ 6, 7, 13], [ 8, 9, 14]]) 可能就好了。在CustomMap类中,添加以下内容:

EventHandler

然后在您的Forms共享代码中订阅InfoTapped事件并推送新的ContentPage:

public event EventHandler InfoTapped;
public virtual void OnInfoTapped(EventArgs e)
{
    EventHandler handler = InfoTapped;
    if (handler != null)
    {
      handler(this, e);
    }
}

现在在渲染器中,创建一个类级别字段来保存对CustomMap的引用:

customMap.InfoTapped += async (sender, e) => {
    await Navigation.PushAsync(new ContentPage());
};

然后在OnElementChanged方法中设置此字段:

CustomMap formsMap;

现在,您可以通过调用:

来引发您在CustomMap中创建的事件
if (e.NewElement != null)
{
      // Remove the var to set your CustomMap field created
      // above so you can use it elsewhere in the class
      formsMap = (CustomMap)e.NewElement;
      ...
}

E.G:

formsMap.OnInfoTapped(e);

您将调用添加到customMap.InfoTapped事件处理程序的代码,在这种情况下将推送新页面。

相关问题