我有个问题。我创建了一个带有基于以下类的表的数据库:
public class Device
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
在我的 App.xaml.cs 中,我执行以下操作:
static List<Device> knownDeviceList;
public App()
{
InitializeComponent();
MyHandler();
MainPage = new NavigationPage(new Page1());
}
public Task MyHandler()
{
return GetKnownDevices();
}
private async Task Get()
{
KnownDeviceList = deviceDatabaseController.GetDevice();
}
public static List<KnownDevice> KnownDeviceList
{
get
{
if (knownDeviceList == null)
{
knownDeviceList = new List<KnownDevice>();
}
return knownDeviceList;
}
set
{
knownDeviceList = value;
}
}
该代码自动运行后,我有一个填充的KnownDeviceList
. 现在在第 1 页中,我使用 ViewModel 创建了一个 ListView,该 ViewModel 使用绑定显示来自 KnownDeviceList 的设备。在同一页面上,我制作了一个按钮来添加新设备,因此它使用NavigationPage
. 但是当我将新设备写入数据库时,我使用:Navigation.PopToRootAsync();
返回,但现在我希望ListView刷新并KnownDeviceList再次从数据库中获取设备,因此我的新设备在ListView中。
我怎样才能做到这一点,因为我不知道添加后该怎么做!?
我的问题是您正在更改集合。但不通知 UI。
List<>
不会自行通知集合更改。凡为ObservableCollection
不通知集合变化,比如Add
,Remove
等故名ObservableCollection
。
使用ObservableCollection<KnownDevice>
而不是List<KnownDevice>
可以解决这个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句