从领域数据库加载我的可观察集合时,我遇到了问题。我创建了一个函数,在该函数中创建此列表,每当列表为null时,它就会创建一个新列表,我的问题是如何设置为读取使用Realm从数据库创建的列表后是否读取?对于使我对此步入正轨的任何帮助,我将不胜感激:)预先感谢您的时间:)
ListViewModel代码:
public PeopleListViewModel(INavigation navigation)
{
this.Navigation = navigation;
realmDb = Realm.GetInstance();
LoadDb();
}
private void LoadDb()
{
var _realmDb = Realm.GetInstance();
Debug.WriteLine("Reading wastes collection");
PeopleCOllection = _realmDb.All<Person>() as ObservableCollection<Person>;
if (PersonCollection == null)
{
Debug.WriteLine("Collection Empty");
CreatePeopleList();
}
SaveDb();
}
private void SaveDb()
{
var _realm = Realm.GetInstance();
_realm.Write(() =>
{
foreach (var person in PeopleCollection)
{
_realm.Add(person);
Debug.WriteLine("Person saved into collection!");
}
});
}
答案 0 :(得分:0)
问题是您不应该执行此强制转换:
PeopleCOllection = _realmDb.All<Person>() as ObservableCollection<Person>;
要从数据库结果中创建可观察的集合,应执行以下操作:
PeopleCOllection = new ObservableCollection<Person>(_realmDb.All<Person>().ToList());