【问题标题】:How to receive feedback from classRenderer如何接收来自 classRenderer 的反馈
【发布时间】:2015-03-15 00:31:02
【问题描述】:

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

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

MapView(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 类中,我还处理了点击 Pins 后显示的按钮的点击,如下所示:

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

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

【问题讨论】:

  • 只是为了理解。您想传递给MyMap 的列表:这是位置列表吗?当你点击一个图钉时,应该在你的MapView 上调用一些方法
  • 列表为Fiiale类型示例中声明的列表。并且调用的方法在MyMapDelegate上,CalloutAccessoryTapped

标签: c# xamarin xamarin.forms


【解决方案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 或类似内容中的模式。

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

x.PinTapped.Execute("YourParameter");

【讨论】:

  • 您应该能够通过将Command 添加到您在单击按钮时调用的 MyMap 来完成其余的工作
  • @DQuaglio 我已经更新了答案。希望对你有帮助
  • @DQuaglio 我添加了代码来展示如何从 iOS 访问 Locations 和 PinTapped
  • @Dquaglio 答案已更新。希望能帮助到你。我目前没有可用的 Mac,所以我猜测了一下
  • 现在可以用了,但是在执行app的时候连MapiOS都进不去,地图也没有显示,MyMap有问题..
猜你喜欢
  • 1970-01-01
  • 2015-03-16
  • 2012-08-27
  • 2016-05-26
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多