Event & Command
Event 和Command是程序內部通信的基礎。Routed events 能夠發起多重控件,並且能有序和用戶輸入溝通。Commands是.NET Framework提供的核心構架,來激活和去激活高級別任務。Animation是events的更進一步,讓你能夠以友好交互的方式使用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 種類
l direct, bubbling, and tunneling
l Direct events是最像普通.net事件的。是有其定義的control發起,其他control不能控制它,例如:MouseLeave event.
l Bubbling events是從子控件開始,逐步影響所有的父控件。例如MouseDown依次從Label,FlowPanel,window 逐步raised。所有控件都可以做Bubbling events
l Tunneling events 和Bubbling Events完全不同,他是先從最高層父控件開始,逐步影響其集成關系的控件,最后到其定義子控件。例如:PreviewMouseDown。Tuneling event can let you intercept and handle the event, so you can filter input ,例如:keystrokes
l 所有的Tunneling Event都以Preview開頭,例如 PreviewKeyDown,PreviewMouseDown。Tunneling Event都是和bubbling Event成對被定義,例如:PreviewKeyDown和KeyDown是一起出現。這個造成了 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
l 在XAML中attaching an Event Handler
<Button Height="23" Margin="132,80,70,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">Button</Button>
l 在C#中寫這個function
private void BtnMultiply_Click(object sender, RoutedEventArgs e)
{
}
1.3.2. Attached Events
l 在父元素中定義一個子元素的event,叫做Attached Events
<Grid Button.Click="button_Click">
<Button Height="23" Margin="132,80,70,0" Name="button1"
VerticalAlignment="Top" >Button</Button>
</Grid>
1.3.3. Handling a Tunneling or Bubbling Events
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Tunneling events和Bubbling events(例如PreViewkeyDown和KeyDown)共享了RoutedEventArgs的實例。如果把Tunneling Event的Handled參數設置成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 |
Returns identifiers(an array) for routed events that have been registered to the event system. |
|
Finds all routed event identifiers(an array) for events that are registered with the provided owner type. |
|
Registers a class handler for a particular routed event. |
|
Registers a class handler for a particular routed event, with the option to handle events where event data is already marked handled. |
|
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來執行代碼,響應應用的startup和closure。應用提供了一組事件,這些事件和頁面的瀏覽相關
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加上事件句柄定義:
<Application x:Class="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