当我从 xamarin 中的同步领域数据库中删除项目时出现空引用异常
null reference exception when i delete item from sync realm database in xamarin
我从同步领域数据库中删除任何项目后出现空引用异常。
该项目已从数据库中删除,但会引发异常和崩溃
我不知道它为什么抛出这个异常或者空对象在哪里。
但是当我删除这一行时,异常消失了:
listView.ItemsSource = 员工;
PS : 在线同步领域数据库时出现此异常
public MainPage()
{
InitializeComponent();
Initialize();
listView.ItemsSource = Employees;
}
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>();
Entertainments= _realm.All<Entertainment>();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
void OnDeleteClicked(object sender, EventArgs e)
{
try {
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
这里是异常的截图
THIS SOLVED THE EXCEPTION ! Thank youu . but the listview is not auto updated now !
根据您的代码和描述,您希望从领域数据库中删除项目,并更新列表视图。我发现您使用 PropertyChanged 来更新 Employees,但它不起作用,因为您从领域数据库中删除了项目,不更改 Employees,因此不会触发 PropertyChanged 事件。
List<Employee> Employees = new List<Employee>();
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>().ToList(); ;
Entertainments = _realm.All<Entertainment>();
}
void OnDeleteClicked(object sender, EventArgs e)
{
try
{
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
Employees = _realm.All<Employee>().ToList();
listView.ItemsSource = Employees;
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
对您有帮助的文章:
https://dzone.com/articles/xamarinforms-working-with-realm-database
但是当我删除这一行时,异常消失了: listView.ItemsSource = 员工;
PS : 在线同步领域数据库时出现此异常
public MainPage()
{
InitializeComponent();
Initialize();
listView.ItemsSource = Employees;
}
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>();
Entertainments= _realm.All<Entertainment>();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
void OnDeleteClicked(object sender, EventArgs e)
{
try {
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
这里是异常的截图
THIS SOLVED THE EXCEPTION ! Thank youu . but the listview is not auto updated now !
根据您的代码和描述,您希望从领域数据库中删除项目,并更新列表视图。我发现您使用 PropertyChanged 来更新 Employees,但它不起作用,因为您从领域数据库中删除了项目,不更改 Employees,因此不会触发 PropertyChanged 事件。
List<Employee> Employees = new List<Employee>();
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>().ToList(); ;
Entertainments = _realm.All<Entertainment>();
}
void OnDeleteClicked(object sender, EventArgs e)
{
try
{
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
Employees = _realm.All<Employee>().ToList();
listView.ItemsSource = Employees;
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
对您有帮助的文章: https://dzone.com/articles/xamarinforms-working-with-realm-database