ios - MvvmCross iOS : How to bind MapView Annotation to jump to another view?

标签 ios xamarin mvvmcross

当单击标注附件按钮时,如何绑定(bind) MapView 的注释以切换到不同的 View ?如何实现注释的 CalloutAccessoryControlTapped 方法?

或者最好的方法是什么?

这是我的代码:

[Register("MapView")]
public class MapView : MvxViewController
{

    public override void ViewDidLoad()
    {
        Title = "Map";
        base.ViewDidLoad();

        var mapView = new MKMapView(new RectangleF(0, 0, 320, UIScreen.MainScreen.Bounds.Height - 20 - 44))
            {
                MapType = MKMapType.Standard,
                ZoomEnabled = true,
                ScrollEnabled = true,
                Delegate = new MapDelegate(),
            };
        View.AddSubview(mapView);

        var center = new CLLocationCoordinate2D(ViewModel.CurrentLat, ViewModel.CurrentLong);
        var span = new MKCoordinateSpan(5.0, 5.0);
        var region = new MKCoordinateRegion(center, span);
        mapView.SetRegion(region, true);

        mapView.AddAnnotation(CreateNewAnnotation(ViewModel.CurrentLat, ViewModel.CurrentLong, "You are here"));

        var set = this.CreateBindingSet<MapView, MapViewModel>();
        set.Bind(mapView).For(???).To(vm => vm.showDetailCommand); // how can I bind to the map annotation and switch to other view when user click it?
        set.Apply();
    }

    protected class MapDelegate : MKMapViewDelegate
    {
        public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, MonoTouch.Foundation.NSObject annotation)
        {
            var pinId = "locationPinId";
            var pinView = (LocationAnnotationView)mapView.DequeueReusableAnnotation(pinId) ??
                          new LocationAnnotationView
                    {
                        Annotation = annotation,
                        RestorationIdentifier = pinId,
                        Image = UIImage.FromBundle("images/map_pointer_icon_small.png"),
                    };

                var buttonView = new UIButton(new RectangleF(0, 0, 27, 27));
                buttonView.SetImage(UIImage.FromBundle("images/blue_arrow.png"), UIControlState.Normal);
                buttonView.Tag = 88888;
                pinView.RightCalloutAccessoryView = buttonView;

            return pinView;
        }

        public override void CalloutAccessoryControlTapped(MKMapView mapView, MKAnnotationView view, UIControl control)
        {
            // how can I bind this method, so when the user click on the annotation it can switch to other view?
        }
    }
}

最佳答案

您可以通过多种方式做到这一点。

鉴于您已经在使用固定的非绑定(bind)(非更新)单个项目显示,那么最简单的方法可能是您扩展注释,以便使用 ICommand 创建它以及纬度、经度和标签:

CreateNewAnnotation(
  ViewModel.CurrentLat, 
  ViewModel.CurrentLong, 
  ViewModel.ShowDetailCommand, 
  "You are here")

完成后,ICommand:

  • 然后可以轻松地从您的 callout、buttonView 的 TouchUpInside 或您的 CalloutAccessoryControlTapped 处理程序中调用。
  • 可以在 ShowDetailCommand 内部实现为导航命令 - 请参阅 http://mvvmcross.wordpress.com/ 中的 N=5有关导航的示例。

如果您确实想要一个更加动态的注释 - 具有真正的数据绑定(bind),那么您需要开始考虑将数据绑定(bind)添加到您的自定义 Annotation 类、您的自定义 AnnotationView 以及您的自定义 MapViewDelegate - 这类似于将数据绑定(bind)添加到 UIView 的方式 - 请参阅 MvxView.cs - 但对于您当前的示例来说可能过度杀伤

关于ios - MvvmCross iOS : How to bind MapView Annotation to jump to another view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756846/

相关文章:

c# - subview 模型更新时如何更新父 View 模型

android - 如何将页脚菜单停靠在 Appcelerator Titanium 的底部?

ios - 协议(protocol)类别和传递信息

javascript - 如何在 Swift 中获取 UIWebView 中的 JS 值?

Azure API 应用程序如何为移动和 Web 使用单一托管 api

xamarin - 在 C# 中使用 Xamarin Forms 自动完成

ios - MvxTabBarViewController标签栏色调颜色

crash - 导航到新 View 时Windows Phone 8.1访问冲突

c# - 需要来自苹果的 iOS 崩溃日志的建议

c# - 在 Xamarin 中将 cookie 放在 WebView 上