首页 > 解决方案 > Xamarin 映射 observablecollection 不更新视图

问题描述

感谢Bind to Xamarin.Forms.Maps.Map from ViewModel我制作了一个自定义地图。但是当我从视图模型中添加引脚时,我在视图中看不到它们。

<maps:BindableMap MapSpan="{Binding MapSpan}" PinsSource="{Binding Pins}"/>

和视图模型代码

        public ObservableCollection<Pin> Pins
        {
            get
            {
                return _pins;
            }
            set
            {
                _pins = value;
            }
        }

 Pins.Add(new Pin { Address=address, Position =position, Label=label, Type=type});

还是看不到别针。。

标签: xamarin.forms

解决方案


不要在非官方文件上做自定义地图。转到官方网站: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map-pin

和自定义渲染器(取决于你想要做什么) https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/customrenderers-map-pin/

然后按照 StackOverflow 问题进行操作。也不要将一半的​​代码放在 xaml 中,一半放在 C# 中,这只会使你的代码变得复杂,放置 c# 替代方案或 xaml 替代方案。不要将两者混合。它只会产生错误、错误或混乱。

和自定义地图:

public class CustomMap : Map
{
    public ObservableCollection<CustomPin> CustomPins { get; set; }
}

应该这样做。


推荐阅读