我有一个JSON类文件,其中包含三个类,所有这些类都遵循以下结构:

public class ManifestJSON : INotifyPropertyChanged
{
    [JsonProperty("dataType")]
    private string dataType;

    public string DataType
    {
        get
        {
            return dataType;
        }
        set
        {
            if(dataType != value)
            {
                dataType = value;
                RaisePropertyChanged("DataType");
            }
        }
    }

    [JsonProperty("ttl")]
    private int time_to_live;

    public int Time_To_Live
    {
        get
        {
            return time_to_live;
        }
        set
        {
            if (time_to_live != value)
            {
                time_to_live = value;
                RaisePropertyChanged("Time_To_Live");
            }
        }
    }

    [JsonProperty("serial")]
    private long serial;

    public long Serial
    {
        get
        {
            return serial;
        }
        set
        {
            if (serial != value)
            {
                serial = value;
                RaisePropertyChanged("Serial");
            }
        }
    }

    [JsonProperty("modifiedIso8601")]
    private string modifiedIso8601;

    public string ModifiedIso8601
    {
        get
        {
            return modifiedIso8601;
        }
        set
        {
            if (modifiedIso8601 != value)
            {
                modifiedIso8601 = value;
                RaisePropertyChanged("ModifiedIso8601");
            }
        }
    }

    [JsonProperty("modifiedTimestamp")]
    private long modifiedTimestamp;

    public long ModifiedTimestamp
    {
        get
        {
            return modifiedTimestamp;
        }
        set
        {
            if (modifiedTimestamp != value)
            {
                modifiedTimestamp = value;
                RaisePropertyChanged("ModifiedTimestamp");
            }
        }
    }

    [JsonProperty("timezone")]
    private string timezone;

    public string Timezone
    {
        get
        {
            return timezone;
        }
        set
        {
            if (timezone != value)
            {
                timezone = value;
                RaisePropertyChanged("Timezone");
            }
        }
    }

    [JsonProperty("exports")]
    private ObservableCollection<ManifestItem> manifest_Items;

    public ObservableCollection<ManifestItem> Manifest_Items
    {
        get
        {
            return manifest_Items;
        }
        set
        {
            if (manifest_Items != value)
            {
                manifest_Items = value;
                RaisePropertyChanged("Manifest_Items");
            }
        }
    }

    //Event handling
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        Console.WriteLine("Updated");
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }
}


在另一个类中,我创建了一个ManifestJSON类型的全局实例。

public ManifestJSON manifestData;


通过使用Newtonsoft.json库中的DeserializeObject方法将JSON字符串反序列化到该对象中来填充该对象,如下所示:

manifestData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString)

这将成功填充ManifestJSON类,但是没有任何属性方法或事件被触发。我在这里做错了什么?

最佳答案

如果要更新现有数据绑定的ManifestJSON对象,则不应使用新对象替换该对象,而应将JSON字符串反序列化为新对象,然后设置现有manifestData对象的属性:

var newData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString);
manifestData.DataType = newData.DataType;
manifestData.Time_To_Live = newData.Time_To_Live;
manifestData.Serial = newData.Serial;
//...

10-07 19:45