PropertyChanged事件没有冒泡C#

时间:2017-09-19 05:36:46

标签: c# xamarin.forms uwp inotifypropertychanged

我正在设计一个可以在屏幕上编辑的简单模型。在这种情况下,它适用于Xamarin Forms UWP,但这个模型类是平台无关的,我怀疑它是UWP / Xamarin特定的问题。我需要代码才能工作,这样如果我编辑了模型中的任何属性,事件应该冒泡到顶部,我应该能够处理UserPreferences上的PropertyChanged事件来保存记录。但是,无论更改哪些属性,此事件都不会被触发。我可以看到属性正在改变。

如果我使用其普通构造函数实例化UserPreferences类,这可以正常工作。但是,这个类实际上是通过反序列化创建的。因此,似乎序列化过程正在为构造函数做一些有趣的事情。

以下是我的课程:

[Serializable]
public class TaskNotificationPreferences : INotifyPropertyChanged
{
    #region Events 
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region Public Properties 
    public ObservableCollection<int> PriorityKeys { get; set; }
    #endregion

    #region Constructor 
    public TaskNotificationPreferences()
    {
        PriorityKeys = new ObservableCollection<int>();
        PriorityKeys.CollectionChanged += PriorityKeys_CollectionChanged;
    }
    #endregion

    #region Event Handlers 
    private void PriorityKeys_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PriorityKeys)));
    }
    #endregion
}

[Serializable]
public class NotificationPreferences : INotifyPropertyChanged
{
    #region Fields
    private bool _ReceivePushNotifications;
    private bool _ReceiveEmails;
    #endregion

    #region Events
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region Constructor 
    public NotificationPreferences()
    {
        TaskNotificationPreferences = new TaskNotificationPreferences();
        TaskNotificationPreferences.PropertyChanged += TaskNotificationPreferences_PropertyChanged;
    }
    #endregion

    #region Event Handlers 
    private void TaskNotificationPreferences_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TaskNotificationPreferences)));
    }
    #endregion

    #region Public Properties
    public TaskNotificationPreferences TaskNotificationPreferences { get; set; }

    public bool ReceivePushNotifications
    {
        get
        {
            return _ReceivePushNotifications;
        }
        set
        {
            _ReceivePushNotifications = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ReceivePushNotifications)));
        }
    }

    public bool ReceiveEmails
    {
        get
        {
            return _ReceiveEmails;
        }
        set
        {
            _ReceiveEmails = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ReceiveEmails)));
        }
    }
    #endregion
}

[Serializable]
public class UserPreferences : INotifyPropertyChanged
{
    #region Events 
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region Public Properties 
    public NotificationPreferences NotificationPreferences { get; set; }
    #endregion

    #region Constructor 
    public UserPreferences()
    {
        NotificationPreferences = new NotificationPreferences();
        NotificationPreferences.PropertyChanged += NotificationPreferences_PropertyChanged;
    }
    #endregion

    #region Event Handlers 
    private void NotificationPreferences_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NotificationPreferences)));
    }
    #endregion
}

如何对类进行反序列化:

    public static SerialiseType DeserialiseObjectWithoutCatch<SerialiseType>(string objectXml)
    {
        var serializer = new XmlSerializer(typeof(SerialiseType));
        var sr = new StringReader(objectXml);
        var retVal = serializer.Deserialize(sr);
        return (SerialiseType)retVal;
    }

修改 最后,这是我得到的解决方法。它有效,但我认为在某种程度上它突出了.NET中序列化的问题。这是我的解决方法代码:

[Serializable]
public class UserPreferences : INotifyPropertyChanged
{
    #region Events 
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region Public Properties 
    public NotificationPreferences NotificationPreferences { get; set; } = new NotificationPreferences();
    #endregion

    #region Event Handlers 
    private void NotificationPreferences_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NotificationPreferences)));
    }
    #endregion

    #region Public Methods
    public void HandlePropertyChanged()
    {
        NotificationPreferences.PropertyChanged += NotificationPreferences_PropertyChanged;
        NotificationPreferences.HandlePropertyChanged();
    }
    #endregion
}


   [Serializable]
    public class NotificationPreferences : INotifyPropertyChanged
    {
        #region Fields
        private bool _ReceivePushNotifications;
        private bool _ReceiveEmails;
        #endregion

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Event Handlers 
        private void TaskNotificationPreferences_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TaskNotificationPreferences)));
        }
        #endregion

        #region Public Properties
        public TaskNotificationPreferences TaskNotificationPreferences { get; set; } = new TaskNotificationPreferences();

        public bool ReceivePushNotifications
        {
            get
            {
                return _ReceivePushNotifications;
            }
            set
            {
                _ReceivePushNotifications = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ReceivePushNotifications)));
            }
        }

        public bool ReceiveEmails
        {
            get
            {
                return _ReceiveEmails;
            }
            set
            {
                _ReceiveEmails = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ReceiveEmails)));
            }
        }
        #endregion

        #region Public Methods
        internal void HandlePropertyChanged()
        {
            TaskNotificationPreferences.PropertyChanged += TaskNotificationPreferences_PropertyChanged;
            TaskNotificationPreferences.HandlePropertyChanged();
        }
        #endregion
    }


   [Serializable]
    public class TaskNotificationPreferences : INotifyPropertyChanged
    {
        #region Events 
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private ObservableCollection<int> _PriorityKeys = new ObservableCollection<int>();

        #region Public Properties 
        public ObservableCollection<int> PriorityKeys
        {
            get { return _PriorityKeys; }
            set
            {
                _PriorityKeys = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PriorityKeys)));
            }
        }
        #endregion

        #region Event Handlers 
        private void PriorityKeys_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PriorityKeys)));
        }
        #endregion

        #region Public Methods
        internal void HandlePropertyChanged()
        {
            PriorityKeys.CollectionChanged += PriorityKeys_CollectionChanged;
        }
        #endregion

    }

所以,我现在的问题是,解决这个问题的最佳方法是什么?这是.NET中的错误吗?序列化中的一个错误?或者,是否应该以某种方式重新设计模型?

0 个答案:

没有答案