Handling OnPropertyChanged

前端 未结 4 1516
南笙
南笙 2020-12-07 16:12

I\'m not well versed in event-based programming. Basically, I\'m still stumbling around with it. I\'m trying to get something set up, but even with the tutorials, I can\'t w

4条回答
  •  臣服心动
    2020-12-07 16:33

    why isn't this just calling PropertyChanged(this, new PropertyCHangedEventArgs(name))

    Because if no one attached an handler to the event, then the PropertyChanged object returns null. So you'll have to ensure it's not null before calling it.

    where does PropertyChanged get assigned?

    In the "listener" classes.

    For example, you could write in other class:

    ChattyClass tmp = new ChattyClass();
    tmp.PropertyChanged += (sender, e) =>
        {
            Console.WriteLine(string.Format("Property {0} has been updated", e.PropertyName));
        };
    

    What does the assignment look like?

    In C# we use the assignment operators += and -= for events. I recommend reading the following article to understand how to write event handlers using the anonymous method form (example above) and the "old" form.

提交回复
热议问题