How do you handle the event raised in your class inside your own class?

I have a "partial" class in VB.NET. Half of it is automatically generated by the code generation tool. This half implements INotifyPropertyChanged, so any properties in this part of the partial class raise the PropertyChanged event.

In my “custom” part of the class, I declare another property that depends on one of the properties on the automatically generated side. Therefore, when this automatically generated property changes, I also want to raise the PropertyChanged event with my custom property, which depends on it.

If I go into the generated part of the class and raise an event there, it will be overwritten if I ever regenerate this part, so I do not want to do this. I would rather add an event handler to my side of the partial class, which checks to see if the generated property has changed, and if so, raise another event for my custom property.

I am trying to write this to bind my own event, but it does not work:

Private Sub MyProperty_PropertyChangedHandler( _
        ByVal sender As Object, ByVal e As PropertyChangedEventArgs _
        ) Handles Me.PropertyChanged
    Select Case e.PropertyName
        Case "AutoGenProperty"
            RaiseEvent PropertyChanged(Me, _
                New PropertyChangedEventArgs("MyProperty"))
    End Select
End Sub

I guess this is because you usually use the WithEvents keyword to tell the compiler that you are subscribing to events from this object. I don’t know how to do this inside a class that really raises an event, or if it is possible.

+3
source share
1

-

AddHandler PropertyChanged, addressof MyProperty_PropertyChangedHandler

"Handles Me.PropertyChanged" ,

"PropertyChanged" - , .

UPDATE

, ctor . , , . , , .

Public Partial MyClass
    private class MyInitialiser
       public sub new(byval myParent as MyClass)
          Addhandler myParent.PropertyChanged, addressof myParent.MyProperty_PropertyChangedHandler
       end sub
    end class
    private _myInitialiser as new MyInitialiser(me)
. . .
End Class

, .

, :)

+2

Source: https://habr.com/ru/post/1706379/


All Articles