Should I create a new event handling method or override the base method?

I use a class derived from the UIElement class when writing a UWP program using C #, where I want to enable processing of input controls such as mouse and keyboard actions. Now I see that there are already virtual methods that say OnSomeEvent() , and I can override this method to fit my processing, or I can create a new method for processing open events defined in the base class and subscribe them to these input events in the constructor. I assume these two methods work, but I hope to find out what is a more professional or more appropriate way of doing this and why. It will also help explain why MS offers these two ways at the same time.

Here are the events and methods of the UIElement class https://msdn.microsoft.com/en-us/library/system.windows.uielement(v=vs.110).aspx#Examples

and citation paragraph

UIElement provides a starting point for arranging feature elements, and also provides virtual methods that produce classes that can be overridden, which can affect the layout rendering behavior of an element and its children. Most of the input and focusing behavior of elements in general is also defined in the UIElement class. This includes events for keyboard, mouse, and stylus input, and related status properties. Many of these events are routable events, and many of the input-related events have both bubbling routing versions as well as tunneling versions of the event. These paired events are usually the events of greatest interest to control authors.

+5
source share
1 answer

In a derived class, I usually override an existing method.

Why? An event handler is less reliable than an override method. For example, external classes can clear event handlers, but they cannot change code in an override. You must seal your own class, although your method may be overridden.

Another point to consider: do I want to change the way I manage? Should I have control over the exact execution time of the code (say, before the base class code, after or instead)? If so, you should use override.

+1
source

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


All Articles