示例#1
0
        /// <summary>
        /// Adds an implementation of the INotifyPropertyChanged interface to the given type.
        /// </summary>
        /// <param name="type">The type declaration</param>
        /// <returns>Type with INotifyPropertyChanged implemented.</returns>
        protected CodeTypeDeclaration ImplementINotifyPropertyChanged(CodeTypeDeclaration type)
        {
            //// This method will implement the INotifyPropertyChanged interface
            //// Here's an example :
            //// public class Customer :INotifyPropertyChanged {
            ////      public PropertyChangedEventHandler PropertyChanged;
            ////      public void RaisePropertyChanged(string propertyName) {
            ////          if( this.PropertyChanged !=null ) {
            ////              this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
            ////          }
            ////      }
            //// }
            CodeThisReferenceExpression thisReference         = new CodeThisReferenceExpression();
            CodeTypeReference           notifyPropertyChanged = Code.TypeRef(typeof(INotifyPropertyChanged));

            // Add the implements INotifyPropertyChanged statement
            // public class Customer :INotifyPropertyChanged {
            type.BaseTypes.Add(notifyPropertyChanged);

            // Add the PropertyChanged event as a field to the type
            // public PropertyChangedEventHandler PropertyChanged;
            CodeMemberEvent propertyChangedEvent = type.AddEvent(Code.TypeRef(typeof(PropertyChangedEventHandler)), "PropertyChanged");

            propertyChangedEvent.ImplementationTypes.Add(notifyPropertyChanged);

            // this.PropertyChanged
            CodeEventReferenceExpression eventFieldReference = new CodeEventReferenceExpression(thisReference, "PropertyChanged");

            // Add the RaisePropertyChanged Method which will invoke the PropertyChanged handler whenever a property value changes
            // if( this.PropertyChanged !=null )
            CodeConditionStatement invokeEventHandlersIfAny = new CodeConditionStatement(new CodeBinaryOperatorExpression(eventFieldReference, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)));

            // this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
            invokeEventHandlersIfAny.TrueStatements.Add(
                new CodeDelegateInvokeExpression(eventFieldReference, new CodeExpression[] { thisReference, new CodeObjectCreateExpression("PropertyChangedEventArgs", new CodeArgumentReferenceExpression("propertyName")) }));

            // public void RaisePropertyChanged
            CodeMemberMethod method = type.AddMethod("RaisePropertyChanged", MemberAttributes.Public);

            method.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "propertyName"));

            ////      public void RaisePropertyChanged(string propertyName) {
            ////          if( this.PropertyChanged !=null ) {
            ////              this.PropertyChanged (this, new PropertyChangedEventArgs( propertyName ) );
            ////          }
            ////      }
            method.Statements.Add(invokeEventHandlersIfAny);
            return(type);
        }