Reputation: 550
I have created a custom map using Xamarin Forms Map. I want to access updated VisibleRegion when VisibleRegion is changed.
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null)
return;
var map = (CustomMap)sender;
if(map.VisibleRegion==null)
return;
map.OnMapRegionUpdated();
}
public MapCenterPosition MapCenter { get; set; }
public static readonly BindableProperty MapCenterProperty = BindableProperty.Create(
nameof(MapCenter),
typeof(MapCenterPosition),
typeof(CustomMap),
null);
public void OnMapRegionUpdated()
{
MapCenter = new MapCenterPosition {Position = VisibleRegion.Center};
}
public class MapCenterPosition: INotifyPropertyChanged
{
private Position _position;
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange([CallerMemberName]string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public Position Position
{
get => _position;
set
{
_position = value;
RaisePropertyChange();
}
}
}
Now I have the CustomMap & binded it to ViewModel code. But how can I trigger some method in my ViewModel when MapCenter is updated ?
Upvotes: 0
Views: 1011
Reputation: 550
I made a simple mistake
All I have to do was to set binding mode as TwoWay in xaml.
And in custom control, I should replace this
public MapCenterPosition MapCenter { get; set; }
with
public MapCenterPosition MapCenter
{
get => (MapCenterPosition) GetValue(MapCenterProperty);
set => SetValue(MapCenterProperty,value);
}
Now I can trigger method in view model every time MapCenter got updated.
Upvotes: 1