2017-02-01 47 views
0

我面临以下问题。Xamarin:从MapRenderer类导航到ContentPage

我正在Xamarin上开发一个跨平台项目,我试图从MapInfoWindow中打开一个ContentPage。 地图ContentPage是便携式项目内部和Droid的项目中我有以下的类,它是在那里我试图打开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; 
    } 
} 
+0

那么你面临的问题是什么?请更新您的问题。 – Demitrian

回答

0

所以我相信建立在你的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); 

EG:

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事件处理程序的代码将被调用,在这种情况下推新页面。