WPF INotifyPropertyChanged类的序列化问题

例如,我有一个这样的类:

[Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            public event PropertyChangedEventHandler PropertyChanged; 
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

编译它会给出一个 错误,即
公共事件 PropertyChangedEventHandler PropertyChanged;无法序列化。

为此,有必要将事件部分指定为NonSerialized,

 [Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            [NonSerialized] 
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }
如果我这样做,我会再次收到错误消息。
NonSerialized 不是字段类型,因此您需要将其正确指定为字段类型。
 [Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            [field: NonSerialized] ///変更点ここ
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

现在您可以使用 INotifyPropertyChanged 序列化类。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值