开发者

Having problem in understanding and creating events in WPF

开发者 https://www.devze.com 2023-03-30 04:42 出处:网络
I am new to WPF, and I am learning using the book Pro WPF C# 2010. Now in the chapter about events, the book explains how to create events and register events in WPF and it gives, as an example, code

I am new to WPF, and I am learning using the book Pro WPF C# 2010.

Now in the chapter about events, the book explains how to create events and register events in WPF and it gives, as an example, code from the ButtonBase class which is derived from some other class which the code doesn't mention clearly.

Now to understand the big picture, i have also tried to create my own simple class, and tried to register an event, which i'll try to raise later (just for my own understanding).

Here is the code I have written:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Input;

    namespace WPFRoutedEvents
    {
            public class EventTest
            {
            private string variable = "Event has occured";

            public static readonly RoutedEvent myTestEvent;

            //Constructor
            public static EventTest()
            {
                EventTest.myTestEvent = EventManager.RegisterRoutedEvent("TestEvent", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof(EventTest));
            }

            public string getVariable()
            {
                return this.variable;
            }

            public event RoutedEventHandler myTestEvent
            {
                add
                {
                      //Here is the problem
                      // I cannot use base.AddHandler () because it does not exist
                }

                remove
                {
                     //Here is the problem
                     // I cannot use base.RemoveHandler() because it does not exist
                }
            }




        }// end of class EventTest
    }

Now since this class has not been derived from any class, i cannot access the base class function AddHandler.

My questions are the following:

1) Which class has the original AddHandler function implemented from which i need to extend my class?

2) Someone please briefly explain the pipeline from wr开发者_如何学Pythoniting and event to finally setting it up to the point where it'll call the handler when it occurs, i.e starting from implementing an event, event handler, registering an event, defining the syntax of the event handler (coding part), and the pipeline from when en event occurs to until it is handled (actual execution of an event and event handler). It'll add more to my understanding of the text.

3) What i find in the book is ClickEvent which has already been implemented somewhere, it is created, registered, and finally handled. What i want to know is how will a programmer register some new type of event which has not yet been implemented (example could be triple click, just for the sake of example, otherwise i know it exists), and then register that event and then design a handler.

4) Is there any type of events, or some other equivalent, that occur on a particular state of a data (some variable, resource etc) instead of some input device event? Like for example while dragging to draw a line, it reaches a particular length?

Thanks!


The RoutedEvent system is centered around the UIElement class and its AddHandler method.
You should only create routed events in classes that inherit UIElement.

All other classes should use ordinary CLR events.


When you register a routed event, UIElement will prepare to store handlers for the event in instances of your class.
When you call AddHandler, UIElement will add the handler to something like a Dictionary<RoutedEvent, Delegate>.
When you add an event handler in XAML, the generated MyFile.xaml.g.cs code will add the handler using the CLR event accessor. When you call RaiseEvent, UIElement will loop through its dictionary and call each handler.

For more information about field-like and custom events in C#, see my blog.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号