C# UWP listview没有';t在更新/向MVVM集合列表添加元素后更新

C# UWP listview没有';t在更新/向MVVM集合列表添加元素后更新,c#,listview,mvvm,uwp,win-universal-app,C#,Listview,Mvvm,Uwp,Win Universal App,我对listview ItemSource和MVVM绑定感到失望。我正在用数据模型绑定listview。加载listview后,我向listview添加了另一个项目,更新没有反映在listview中,但它显示列表集合中的项目数增加了一个。如果在构造函数的列表集合中添加外接程序,它将显示在listview中 XAML 控制器 public class VictimsController : INotifyPropertyChanged { List<VictimProfile&

我对listview ItemSource和MVVM绑定感到失望。我正在用数据模型绑定listview。加载listview后,我向listview添加了另一个项目,更新没有反映在listview中,但它显示列表集合中的项目数增加了一个。如果在构造函数的列表集合中添加外接程序,它将显示在listview中

XAML


控制器

public class VictimsController : INotifyPropertyChanged
{
    List<VictimProfile> _victims = new List<VictimProfile>();
    public VictimsController()
    {
        //Victims.Add(new VictimProfile() { GPSLatitute = 123, GPSLongtitude = 2333 });
    }
    public List<VictimProfile> VictimList
    {
        get
        {
            return _victims;
        }
        set
        {
            _victims = value;
            OnPropertyChanged("VictimList");
        }
    }
    public void addVictim(VictimProfile profile)
    {
        _victims.Add(profile);
        OnPropertyChanged("VictimList");
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共类受害者控制器:INotifyPropertyChanged
{
列表_受害者=新列表();
公众受害者控制员()
{
//添加(新的VictimProfile(){GPSLatitute=123,GPSLongtitude=2333});
}
公众名单受害者名单
{
得到
{
遣返受害者;
}
设置
{
_受害者=价值;
财产变更(“受害者名单”);
}
}
公共无效添加受害者(受害者档案)
{
_增加(概况);
财产变更(“受害者名单”);
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
向listview模型或集合添加一项

async void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        string ReceivedMessage = Encoding.UTF8.GetString(e.Message);
        //convert the message to json format
        var payLoad = JsonConvert.DeserializeObject<VictimProfile>(ReceivedMessage);
        // we need this construction because the receiving code in the library and the UI with textbox run on different threads
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
            txt.Text += ReceivedMessage;
            VictimsController model = this.DataContext as VictimsController;
            model.addVictim(payLoad); //add item to the list collection
            Debug.WriteLine("count: " + ls.Items.Count); //this shows that listview has one more item added to it, but nothing is shown in the listview
            //ls.Items.Add(payLoad);
        });
    }
异步无效客户端\u MQTTMSGPPublishReceived(对象发送方,MQTTMSGPPublishEventArgs e)
{
string ReceivedMessage=Encoding.UTF8.GetString(e.Message);
//将消息转换为json格式
var payLoad=JsonConvert.DeserializeObject(ReceivedMessage);
//我们需要这种构造,因为库中的接收代码和带有textbox的UI在不同的线程上运行
wait Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{
txt.Text+=ReceivedMessage;
VictimsController模型=this.DataContext作为VictimsController;
model.addVisum(有效负载);//将项添加到列表集合
Debug.WriteLine(“count:+ls.Items.count);//这表明listview添加了一个以上的项,但listview中没有显示任何内容
//ls.项目。添加(有效载荷);
});
}
关键是列表集合有新项,而ls(listview)也有该项,但它没有显示在listview中。

最后我解决了它。 将列表更改为可观察集合解决了这个问题

 ObservableCollection<VictimProfile> _victims = new ObservableCollection<VictimProfile>();
observedcollection\u受害者=新的observedcollection();
 ObservableCollection<VictimProfile> _victims = new ObservableCollection<VictimProfile>();