如何接收来自 classRenderer 的反馈

How to receive feedback from classRenderer

我需要在使用 Xamarin Forms 开发的项目中使用自定义渲染器实现地图,我是这样做的:

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

地图视图(Xamarin 表单):

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 class 中,我还处理了点击 Pin 图显示的按钮,如下所示:

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

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

将您的 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 或类似内容中的模式。

你现在有了命令,所以把它带到你的 MyMapDelegate 并使用

x.PinTapped.Execute("YourParameter");