c# - Xamarin C# - INotifyPropertyChanged 模型 - JSON 反序列化错误
问题描述
我有与 Azure 移动服务同步的 ac# 用户模型。User类的定义如下:
public class Users : INotifyPropertyChanged
{
[JsonProperty("Id")]
private string id;
[JsonIgnore]
public string Id
{
get { return id; }
set { id = value; OnPropertyChanged("Id"); }
}
[JsonProperty("Name")]
private string name;
[JsonIgnore]
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
[JsonProperty("Email")]
private string email;
[JsonIgnore]
public string Email
{
get { return email; }
set { email = value; OnPropertyChanged("Email"); }
}
[JsonProperty("Password")]
private string password;
[JsonIgnore]
public string Password
{
get { return password; }
set { password = value; OnPropertyChanged("Password"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if(propertyName != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
现在,在 XAML 页面代码中,一旦用户登录,我就会尝试获取用户,如下所示:
var user = (await App.MobileService.GetTable<Users>().Where(u => u.Email == this.txtEmail.Text).ToListAsync()).FirstOrDefault();
如果我删除私有和公共成员上的 JsonProperty() 和 JsonIgnore(),我会收到错误:NewtonSoft.Json 反序列化错误。
在上面的代码中,除了分别在每个私有和公共字段上声明 JsonProperty() 和 JsonIgnore 之外,还有什么更简单的方法吗?
解决方案
推荐阅读
- angular - Angular 11 TypeError:无法添加属性 x,对象不可扩展
- excel - 我想在 excel 中使用 VBA 来更改我正在使用的数据的格式,以匹配我正在加载的系统
- javascript - 错误:ENOENT:没有这样的文件或目录。为什么还有?
- c# - 不理解 JSON Web Tokens 的东西
- reactjs - 在 setState React 和 socketIO 中发布了重复的字母
- flutter - 使用 widget.[variable_name] 是引用 StatefulWidget 变量的最佳方式吗?
- java - 无法作为客户端连接到服务器时不会抛出异常
- javascript - 需要帮助将工作箭头函数转换为常规函数以编写单元测试
- python - 我在硒中找不到这个词
- javascript - 如何有效地通过 React 中父级的引用访问子级?