2014-11-05 41 views
0

我试图让MKMapKit注释在Xamarin iOS项目中实时更新。如何获得MvvmCross MKAnnotation实时更新绑定

我正在使用MvvmCross,并基于@slodge代码的实现,它的工作很好。

https://gist.github.com/slodge/6070386

我想什么现在能够做的是什么斯图尔特暗示在他的评论之一。

public class HouseAnnotation : MKAnnotation 
{ 
    public HouseAnnotation(House house) 
    { 
     // use house here... 
     // in theory you could also data-bind to the house too (e.g. if it's location were to move...) 
    } 

    public override CLLocationCoordinate2D Coordinate { get; set; } 
} 

我怎么会去绑定House坐标到HouseAnnotation.Coordinate

到目前为止,我一直在做绑定,如:

var bindingSet = this.CreateBindingSet<View, ViewModel>(); 

,你这样做在viewDidLoad中,并有机会获得你需要的一切其中工程直线前进。

我觉得像我自然要做到

var bindingSet = myView.CreateBindingSet<HouseAnnotation, House>(); 

但这意味着传递到myView参考下到HouseAnnotation因此它可以用来调用它CreateBindingSet,我怀疑这会甚至工作因为House和HouseAnnotation不是任何Mvx基类的子类。

我觉得我在这里错过了一些难题。有人能帮助我吗?

我知道房子不太可能移动,但我正在为所有可能发生的事情做准备!

+1

有一个N + 1级的视频覆盖此 - ”寻找僵尸(虽然我认为地图更新代码需要对iOS8上的一些变化) – Stuart 2014-11-05 20:47:43

+0

@slodge感谢的是,正是我寻找。到目前为止,我还没有看到iOS 8上的任何问题。地图引脚似乎正在按照预期进行更新和动画处理。 – Justyn 2014-11-06 12:07:30

回答

1

您可以通过使用WeakSubscribe

答案是N + 38左右24mins订阅的house.Location属性更改。

https://www.youtube.com/watch?v=JtXXmS3oHHY

public class HouseAnnotation : MKAnnotation 
{ 
    private House _house; 

    public HouseAnnotation(House house) 
    { 
     // Create a local reference 
     _house = house; 
     // We update now so the annotation Coordinate is set first time round 
     UpdateLocation() 
     // Subscribe to be notified of changes to the Location property to trigger the UpdateLocation method 
     _house.WeakSubscribe<House>("Location", (s, e) => UpdateLocation()); 
    } 

    private void UpdateLocation() 
    { 
     // Convert our house.Location to a CLLocationCoordinate2D and set it on the MKAnnotation.Coordinate property 
     Coordinate = new CLLocationCoordinate2D(_house.Location.Lat, _house.Location.Lng); 
    } 

    public override CLLocationCoordinate2D Coordinate { 
     get { 
      return coord; 
     } 
     set { 
      // call WillChangeValue and DidChangeValue to use KVO with 
      // an MKAnnotation so that setting the coordinate on the 
      // annotation instance causes the associated annotation 
      // view to move to the new location. 

      // We animate it as well for a smooth transition 
      UIView.Animate(0.25,() => 
       { 
         WillChangeValue ("coordinate"); 
         coord = value; 
         DidChangeValue ("coordinate"); 
       }); 
     } 
    } 
}