I'm trying to change MapView in my Xamarin Forms app (with Mapsui and Prism), as I need separate view to store Pins. First MapView (let's call it default
) is for displaying all pins from list. Second MapView (history
) is for displaying new pins, which are removed when leaving Page.
I have Dictionary
for storing my MapViews to have easy way to access any view I want. Every created MapView has the same instance of Map. I want to change from default
to history
which should hide pins that are added in default
view.
I change view with this:
public void ChangeView(string name)
{
var prev = MapView;
loggerService.Info($"Pins: {prev.Pins.Count}");
MapView = GetView(name); // Current MapView in XAML, returns instance of MapView
loggerService.Info($"New view pins: {MapView.Pins.Count}");
Task.Factory.StartNew(() =>
{
Task.Delay(2000).Wait();
loggerService.Info($"View pins: {MapView.Pins.Count}");
});
MapView.Refresh();
}
extra code with log is for debugging purposes - it shows correct values (prev pins = 7, new pins = 0, view pins = 0).
I've added history
view without Zoom buttons to make sure it is changing - and it is (so there is no need to post XAML, I think).
How should I change MapView to hide pins when navigating to history and show them when I switch back to default view? Or is there a better way to 'group' Pins and hide/show them basing on name?
Update I think this may be important to mention:
public Pin AddMarker(Position point)
{
var pin = new Pin(MapView)
{
Label = "PinType.Pin",
Position = point,
Type = PinType.Pin,
Transparency = 0.5f,
Color = Xamarin.Forms.Color.FromRgb(2, 144, 210),
Scale = 0.5f,
};
MapView.Pins.Add(pin);
return pin;
}
This is the way I add Pins to view. This is the same MapView as above (all the code is from MapService
).