Bind to Xamarin.Forms.Maps.Map from ViewModel

后端 未结 5 1593
失恋的感觉
失恋的感觉 2020-12-29 12:49

I\'m working on a Xamarin.Forms app using a page that displays a map. The XAML is:


    ...

<
5条回答
  •  生来不讨喜
    2020-12-29 13:30

    It is not ideal, but you could listen for the property changed event in the code behind and then apply the change from there. Its a bit manual, but it is doable.

    ((ViewModels.YourViewModel)BindingContext).PropertyChanged += yourPropertyChanged;
    

    And then define the "yourPropertyChanged" method

    private void yourPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if(e.PropertyName == "YourPropertyName")
        {
            var position = new Position(37.79762, -122.40181);
            Map.MoveToRegion(new MapSpan(position, 0.01, 0.01));
            Map.Pins.Add(new Pin
            {
                Label = "Xamarin",
                Position = position
            });
        }
    }
    

提交回复
热议问题