0

我面临以下问题。

我正在 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;
    }
}
4

1 回答 1

1

所以我相信在你的 CustomMap 类中创建一个EventHandler可能是要走的路。在您的 CustomMap 类中,添加以下内容:

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

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

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

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

CustomMap formsMap;

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

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;
      ...
}

现在您可以通过调用来引发您在 CustomMap 中创建的事件:

formsMap.OnInfoTapped(e);

例如:

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

    formsMap.OnInfoTapped(e);
}

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

于 2017-02-01T22:31:10.220 回答