如何获得MvvvmCross MKAnnotation实时更新绑定



我正试图在Xamarin iOS项目中实时更新MKMapKit注释。

我正在使用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和HouseAnnion不是任何Mvx基类的子类。

我觉得我遗漏了一点这个谜题。有人能帮我吗?

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

您可以订阅房屋的更改。使用WeakSubscribe 的位置属性

答案是在24分钟左右的n+38。

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");
                });
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新