分享

WPF Event & Command 之一:Event

 牛人的尾巴 2015-12-01

Event & Command

Event Command是程序内部通信的基础。Routed events 能够发起多重控件,并且能有序和用户输入沟通。Commands.NET Framework提供的核心构架,来激活和去激活高级别任务。Animationevents的更进一步,让你能够以友好交互的方式使用event构架,来使用多重控件。

1.1. Configuring Events and Event Handling

routed event architecture WPF中比较特殊的Event构架,和其他技术中Event事件不同。routed event让从某个control中定义event,能够被这个control的父control所发起。例如:一个在toolbar中的button control可以被button, toolbar, grid, window 发起。

应用情景:一个计算器程序,某些BUTTON可以有自己的独特的作用,但是有一个需求,在点击某个控件时,所有的BUTTON都需要有同一个click Event,为了少写些代码,可以用这个,只要在一个地方写click Event,所有在这个Windows里的button都可以有这个click event.

1.2. Types of Routed Events 种类

direct, bubbling, and tunneling

Direct events是最像普通.net事件的。是有其定义的control发起,其他control不能控制它,例如:MouseLeave event.

Bubbling events是从子控件开始,逐步影响所有的父控件。例如MouseDown依次从LabelFlowPanelwindow 逐步raised。所有控件都可以做Bubbling events

Tunneling events Bubbling Events完全不同,他是先从最高层父控件开始,逐步影响其集成关系的控件,最后到其定义子控件。例如:PreviewMouseDownTuneling event can let you intercept and handle the event, so you can filter input ,例如:keystrokes

所有的Tunneling Event都以Preview开头,例如 PreviewKeyDownPreviewMouseDownTunneling Event都是和bubbling Event成对被定义,例如:PreviewKeyDownKeyDown是一起出现。这个造成了 Tunneling Event和他的结对Bubbling Events共享了事件实例中的参数。

1.3. RoutedEventArgs参数

 

 

Handled

Indicates whether this event has been handled. By setting this property to True, you can halt further event bubbling or tunneling

OriginalSource

Returns the RoutedEvent object for the event that was raised. When handling more than one event with the same event handler, you might need to refer to this property to identify which event has been raised.

RoutedEvent

RoutedEventArgs

Source

Returns the object that raised the event.

 

 

1.3.1. Attaching an Event Handler

XAMLattaching an Event Handler

C#中写这个function

        private void BtnMultiply_Click(object sender, RoutedEventArgs e)

        {

          

        }

1.3.2. Attached Events

在父元素中定义一个子元素的event,叫做Attached Events

 

1.3.3. Handling a Tunneling or Bubbling Events

private void textBox1_KeyDown(object sender, KeyEventArgs e)

{

e.Handled = true;

}

Tunneling eventsBubbling events(例如PreViewkeyDownKeyDown)共享了RoutedEventArgs的实例。如果把Tunneling EventHandled参数设置成true,其对应的bubbling event也同样是抑制处理。

 

1.4. The EventManager Class 事件管理类

EventManager is a static class that manages the registration of all WPF routed events.

public static class EventManager

 

继承关系

System.Object   System.Windows.EventManager

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)

 

Name

Description

GetRoutedEvents

Returns identifiers(an array) for routed events that have been registered to the event system.

GetRoutedEventsForOwner

Finds all routed event identifiers(an array) for events that are registered with the provided owner type.

RegisterClassHandler(Type, RoutedEvent, Delegate)

Registers a class handler for a particular routed event.

RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean)

Registers a class handler for a particular routed event, with the option to handle events where event data is already marked handled.

RegisterRoutedEvent

Registers a new routed event with the Windows Presentation Foundation (WPF) event system.

 

下面讲事件类怎么用:

1.4.1. Defining a New Routed Event 定义新的路由事件

1.       创建一个static, read-only definition for the event

public static readonly RoutedEvent SuperClickEvent;

2.       创建一个 wrapper for the routed event that exposes it as a traditional .NET Framework event

public event RoutedEventHandler SuperClick

{

               add

               {this.AddHandler(SuperClickEvent, value);}

               remove

               {this.RemoveHandler(SuperClickEvent, value);}

}

RoutedEventArgs使用EventArgs类。必须从RoutedEventArgs派生一个新类,并创建一个新的委托,使用这些事件参数。

 

3.       使用EventManager来在类构架中注册一个新事件来包含这个事件。必须提供the name of the event, the routing strategy (direct, tunneling,or bubbling), the type of delegate that handles

EventManager.RegisterRoutedEvent("SuperClick",RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Window1));

1.4.2. Raising an Event 发生一个事件

在事件定义之后,你能够发起一个RoutedEventArgs的新实例(使用RaiseEvent方法)

RoutedEventArgs myEventArgs = new RoutedEventArgs(myControl.myNewEvent);

RaiseEvent(myEventArgs);

 

1.4.3. Creating a Class-Level Event Handler 创建Event Handler

使用EventManager的类注册a class-level event handler,这个类级别的Event handler在整个类的实例中处理特定的event,通常在instance handlers之前invoke.因此可以在Instance handlers之前,清理和制止event

1.       Create a static method to handle the event. This method must have the same signature as the event.

private static void SuperClickHandlerMethod(object sender, RoutedEventArgs e)

{

// Handle the event here

}

2.       In the static constructor for the class for which you are creating the class-level event handler, create a delegate to this method.

RoutedEventHandler SuperClickHandler = new RoutedEventHandler(SuperClickHandlerMethod);

3.       Also in the static constructor, call EventManager.RegisterClassHandler to register the class-level event handler,.

EventManager.RegisterClassHandler(typeof(Window1),

SuperClickEvent,SuperClickHandler);

1.5. Application-Level Events 应用级别事件

每一个WPF应用都被Application 对象所包围,提供了一组事件和应用的生命周期相关。你能够处理这些events来执行代码,响应应用的startupclosure。应用提供了一组事件,这些事件和页面的浏览相关

Event

Description

Activated

Occurs when you switch from another application to your program. It also is raised the first time you show a window.

Deactivated

Occurs when you switch to another program

DispatcherUnhandledException

Raised when an unhandled exception occurs in your

application. You can handle an unhandled exception

in the event handler for this event by setting the

DispatcherUnhandledExceptionEventArgs.Handled

property to True.

Exit

Occurs when the application is shut down for any reason

SessionEnding

Occurs when the Windows session is ending, such as when the user shuts down the computer or logs off

Startup

Occurs as the application is started.

 

应用事件是标准的.NET事件(和routed event)相比,可使用标准的方法穿件应用事件。见下:

1.      Visual Studio中,在Solution Explorer中,右击Application.xaml(或者APP.xaml),查看代码。

2.      C#中创建Startup方法,来处理事件

void App_Startup(object sender, StartupEventArgs e)

{

// Handle the event here

}

3.      XAML中,为Application加上事件句柄定义:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

StartupUri="Window1.xaml" Startup="App_Startup">

PRACTICE

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多