2

コールアウト アクセサリ ボタンがクリックされたときに、MapView の注釈をバインドして別のビューに切り替えるにはどうすればよいですか? アノテーションの 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?
        }
    }
}
4

1 に答える 1

2

これはさまざまな方法で行うことができます。

既に固定の非拘束 (非更新) 単一項目表示を使用していることを考えると、おそらく最も簡単な方法は、アノテーションを拡張しICommandて緯度、経度、およびラベルで作成されるようにすることです。

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

これが完了すると、次のようになりICommandます。

  • その後、コールアウト、buttonView、TouchUpInsideまたはCalloutAccessoryControlTappedハンドラーから簡単に呼び出すことができます。
  • 内部でナビゲーション コマンドとして実装できます。ナビゲーションの例については、 http://mvvmcross.wordpress.com/ShowDetailCommandのN=5 を参照してください。

より動的なアノテーションが必要な場合 (真のデータ バインディングを使用)、カスタム Annotation クラス、カスタム AnnotationView、そしておそらくカスタム MapViewDelegate にもデータ バインディングを追加することを検討する必要があります。これは同様です。データ バインディングが UIView に追加される方法 - MvxView.csを参照してください - しかし、現在の例ではおそらくやり過ぎです。

于 2013-07-21T15:27:53.850 に答える