首页 > 解决方案 > 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 之外,还有什么更简单的方法吗?

标签: c#.netxamarinxamarin.forms

解决方案


推荐阅读