locked
problem in serialize a class that Implements INotifyPropertyChanged RRS feed

  • Question

  • Hello

     

    I have a class used to store some data.

    To be able to bind this class to a control i have to implement the

    INotifyPropertyChanged  but if I implement it  i can not serialize the class any more

    Type 'System.ComponentModel.PropertyChangedEventManager' in Assembly 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable

     

    How can I overcome this problem?

    Friday, July 30, 2010 7:42 AM

Answers

  • Use :
        [Serializable]
        public class MyClass:
          INotifyPropertyChanged
        {
    
          #region INotifyPropertyChanged Members
    
          [field:NonSerialized]
          public event PropertyChangedEventHandler PropertyChanged;
    
          #endregion
        }
    

    or

        [Serializable]
        public class MyClass2 : <br/>
                INotifyPropertyChanged
        {
          [NonSerialized]
          PropertyChangedEventHandler handler;
          #region INotifyPropertyChanged Members
    
          event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
          {
            add { handler += value; }
            remove { handler -= value; }
          }
    
          #endregion
        }
    

    • Marked as answer by Linda Liu Tuesday, August 3, 2010 9:05 AM
    Friday, July 30, 2010 12:22 PM

All replies

  • Use :
        [Serializable]
        public class MyClass:
          INotifyPropertyChanged
        {
    
          #region INotifyPropertyChanged Members
    
          [field:NonSerialized]
          public event PropertyChangedEventHandler PropertyChanged;
    
          #endregion
        }
    

    or

        [Serializable]
        public class MyClass2 : <br/>
                INotifyPropertyChanged
        {
          [NonSerialized]
          PropertyChangedEventHandler handler;
          #region INotifyPropertyChanged Members
    
          event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
          {
            add { handler += value; }
            remove { handler -= value; }
          }
    
          #endregion
        }
    

    • Marked as answer by Linda Liu Tuesday, August 3, 2010 9:05 AM
    Friday, July 30, 2010 12:22 PM
  • Spaccabit is correct.  Here is the VB.Net code:

    <Serializable>
    Private Class myClass
        Implements INotifyPropertyChanged
    
        Private _name As String
    
        <NonSerialized>
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    
        ' http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
        ' This method is called by the Set accessor of each property. 
        ' The CallerMemberName attribute that is applied to the optional propertyName 
        ' parameter causes the property name of the caller to be substituted as an argument. 
        Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
    
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
            'Debug.WriteLine("{0}: myClass.NotifyPropertyChanged({1})", Date.Now, propertyName)
    
        End Sub
    
        Public Property Name As String
            Get
                Return _name
            End Get
            Set(value As String)
                If Not _name = value Then
                    _name = value
                    NotifyPropertyChanged()
                End If
            End Set
        End Property
    
    End Class

    Tuesday, July 8, 2014 7:29 PM