如何从classRenderer接收反馈

时间:2015-01-16 11:49:54

标签: c# xamarin xamarin.forms

我需要使用Xamarin Forms在我的项目中使用自定义渲染器实现地图,以下是我的操作方法:

public class MyMap : Map
    {
        public MyMap ():base(){ }
    }

MapView(Xamarin Forms):

public class MapView:ContentPage
    {
            List<Filiale> list=jM.ReadData ();
            MyMap myMap = new MyMap (//I'd like to pass list here);
            var stack = new StackLayout ();
            myMap.VerticalOptions = LayoutOptions.FillAndExpand;
            myMap.HorizontalOptions = LayoutOptions.FillAndExpand;
            stack.Children.Add (myMap);
            Content = stack;
}

MapRenderer(Xamarin iOS):

[assembly: ExportRenderer (typeof (MyMap), typeof (MapiOS))]
namespace App.iOS
{
    public class MapiOS : MapRenderer
    {
    private MKMapView NativeMap { get { return (this.NativeView as MapRenderer).Control as MKMapView; } }
    public MapiOS ():base()
    {

    }

    protected override void OnElementChanged (ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged (e);
        MyMapDelegate myMapDelegate = new MyMapDelegate ();
        NativeMap.Delegate = myMapDelegate;
        NativeMap.AddAnnotation(new MKPointAnnotation (){
            Title=list[0].nome,
            Coordinate = new CLLocationCoordinate2D (42.364260, -71.120824)
        });
    }
}

myMapDelegate课程中,我还处理了点击按钮所显示的按钮,如下所示:

public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control){
            //Call new Page
        }

现在,当点击此按钮时,我想回到Xamarin Forms并使用它创建一个新页面。我该怎么办?另外,当我创建MyMap对象时,如何传递一些对象?

1 个答案:

答案 0 :(得分:1)

将您的MyMap实施更改为此

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

  public MyMap(List<string> locations)
  {
    Locations = locations;
    PinTapped = new Command(async (x) =>
    {
      await Navigation.PopModalAsync();
      await Navigation.PushAsync(SomeNewPage(x));
    });
  }

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

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

现在,您可以通过稍微更改一下从MapRenderer访问Locations

public class MapiOS : ViewRenderer<MyMap, MKMapView>
{
  protected override void OnElementChanged (ElementChangedEventArgs<MyMap> e)
  {
    base.OnElementChanged (e);

    var map = e.NewElement; // Remember to check for null
    var locations = map.Locations;
    // Do what you want with locations

    var cmd = Map.PinTapped; // Send this along to MyMapDelegate
    var nativeMap = new MKMapView(); // Initiate with relevant parameters
    SetNativeControl(nativeMap)

    MyMapDelegate myMapDelegate = new MyMapDelegate (cmd); // Change constructor here
    nativeMap.Delegate = myMapDelegate;
    nativeMap.AddAnnotation(new MKPointAnnotation (){
        Title=list[0].nome,
        Coordinate = new CLLocationCoordinate2D (42.364260, -71.120824)
    });

  }

我假设您已将地图显示为NavigationPage内的模态或类似内容。

您现在拥有了Command,因此请将其带到您的MyMapDelegate并使用

x.PinTapped.Execute("YourParameter");