【发布时间】:2020-03-10 23:21:48
【问题描述】:
我有问题。我从我的 ViewModel 创建了一个 ListView,其中 itemsource 是一个名为 unknownDeviceList 的列表。这是我的视图模型:
public class VM_AddDeviceList : BindableObject
{
private List<UnknownDevice> _unknownDeviceList;
public List<UnknownDevice> unknownDeviceList
{
get
{
return _unknownDeviceList;
}
set
{
if (_unknownDeviceList != value)
{
_unknownDeviceList = value;
OnPropertyChanged();
}
}
}
public List<UnknownDevice> deviceList_raw;
public VM_AddDeviceList()
{
deviceList_raw = new List<UnknownDevice>();
unknownDeviceList = new List<UnknownDevice>();
MyHandler();
}
private async Task LoadUnknownDeviceList()
{
deviceList_raw = await App.RestService.GetDevices();
foreach (UnknownDevice device in deviceList_raw)
{
bool containsItem = App.knownDeviceList.Any(item => item.MAC == device.MAC);
if (!containsItem)
{
unknownDeviceList.Add(device);
}
}
}
public Task MyHandler()
{
return LoadUnknownDeviceList();
}
}
现在我可以看到 unknownDeviceList 被填充到 foreach 中,但在屏幕上 ListView 保持为空。我究竟做错了什么? 与 async 和 await 有什么关系?
【问题讨论】:
-
使用
ObservableCollection<>而不是List<>。
标签: c# xamarin xamarin.forms xamarin.android xamarin.ios