The easy way
If you just want to set a minimum scale and a maximum scale, the Map
class has MinScale
and MaxScale
properties. Replace your code with the following:
MyMapView.Map.MaxScale = 700;
MyMapView.Map.MinScale = 16500000;
The hard way
You probably don't need this! Use the easy way listed above unless you have a good reason to do something more complicated!
If instead for some reason you really want to track scale changes and then change the scale yourself, you should do it in a different way. Currently you're listening for PropertyChanged
, which is way too broad. One effect is that when the scale changes, your event handler runs, which performs an asynchronous zoom, which generates a scale change before it's finished, which calls your event handler, which performs an asynchronous zoom, which generates a scale change before it's finished, which calls your event handler, which...I could go on and on. Literally. And so will your program, unless you make some changes.
Here's one way to do it:
// Save a variable so you can invoke the EventHandler elsewhere
EventHandler navigationCompletedHandler = (sender, args) =>
{
var s = MyMapView.MapScale;
if (s < 500)
MyMapView.SetViewpointScaleAsync(700);
if (s > 16500000)
MyMapView.SetViewpointScaleAsync(16500000);
};
MyMapView.NavigationCompleted += navigationCompletedHandler;
// Invoke the above handler one time when the map first loads
EventHandler firstViewpointChangeHandler = null;
firstViewpointChangeHandler = (sender, args) =>
{
if (!double.IsNaN(MyMapView.MapScale))
{
MyMapView.ViewpointChanged -= firstViewpointChangeHandler;
navigationCompletedHandler.Invoke(null, null);
}
};
MyMapView.ViewpointChanged += firstViewpointChangeHandler;