2017-02-01 9 views
0

У меня возникает следующая проблема.Xamarin: переход от класса MapRenderer к ContentPage

Я разрабатываю проект Cross Platform на Xamarin, и я пытаюсь открыть ContentPage из MapInfoWindow. Карта 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 будет называться, в этом случае нажать новую страницу.

Смежные вопросы