欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
WPF自定義路由事件(二)

WPF自定義路由事件(二)

WPF中的路由事件 as U know,和以前Windows消息事件區別不再多講,這篇博文中,將首先回顧下WPF內置的路由事件的用法,然后在此基礎上自定義一個(gè)路由事件。

1.WPF內置路由事件  

WPF中的大多數事件都是路由事件,WPF有3中路由策略:

具體不多講,單需要注意的是WPF路由事件是沿著(zhù)VIsualTree傳遞的。VisualTree與LogicalTree的區別在于:LogicalTree的葉子節點(diǎn)是構成用戶(hù)界面的控件(xaml緊密相關(guān)),而VisualTree要連控件中的細微結構也算上。VisualTree是LogicalTree的擴展。

reference: Understanding the Visual Tree and Logical Tree in WPF

下面給出一個(gè)使用WPF內置路由事件的例子:

<Window x:Class="WPFRoutedEvent.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525" >    <Grid x:Name="Grid1" Margin="10" Background="AliceBlue"  MouseLeftButtonDown="Grid1_MouseLeftButtonDown">                <StackPanel Background="BurlyWood" Height="200" x:Name="StackPanel1" Button.Click="ButtonInStackPanel_Click"  MouseLeftButtonDown="StackPanel1_MouseLeftButtonDown">            <Button x:Name="Button1" Content="RoutedEvent" Click="Button1_Click" />        </StackPanel>    </Grid></Window>
using System.Windows;using System.Windows.Controls;using System.Windows.Input;namespace WPFRoutedEvent{    /// <summary>    /// MainWindow.xaml 的交互邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            //Grid訂閱Button的Click事件            Grid1.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonInGrid_Click));        }        private void Button1_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("Button  Clicked.");            //            //e.Handled = true;        }        private void ButtonInStackPanel_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("StackPanel Clicked.");        }        private void ButtonInGrid_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("Grid Clicked.");        }        private void Grid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            MessageBox.Show("Grid Mouse Left button down.");        }        private void StackPanel1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            MessageBox.Show("StackPanel Mouse Left button down.");        }    }}

Button的Click事件是一個(gè)路由事件,分別在StackPanel中和Grid中訂閱這個(gè)事件并進(jìn)行相應的處理,分別用xaml代碼和C#代碼如下:

Click="Button1_Click"
Button.Click="ButtonInStackPanel_Click"
Grid1.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonInGrid_Click));

StackPanel的MouseLeftButtonDown也是一個(gè)路由事件,也可以叫“附加事件”。其實(shí)“附加事件”也是路由事件,只是個(gè)文字游戲,為什么還要另外起個(gè)名字呢?原來(lái)路由事件的宿主都是那些擁有可視化實(shí)體的界面元素;而附加事件則不具備顯示在用戶(hù)界面上的能力。

常見(jiàn)的附加事件有:

Binding類(lèi):SourceUpdated事件、TargetUpdated事件。

Mouse類(lèi):MouseEnter事件、MouseLeave事件、MouseDown事件、MouseUp事件等。

Keyboard類(lèi):KeyDown事件、KeyUp事件等。

Grid和StackPanel中均如下訂閱:

MouseLeftButtonDown="StackPanel1_MouseLeftButtonDown"

程序運行如下:

 2.自定義路由事件    

 前面DebugLZQ寫(xiě)過(guò)一篇博文,內容是關(guān)于自定義CLR事件的,參考:.NET自定義事件小結。下面來(lái)自定義一個(gè)WPF路由事件,各位博友可以比較下兩者的異同。

創(chuàng )建自定義路由事件大體可以分為三個(gè)步驟:

(1)聲明并注冊路由事件

(2)為路由事件添加CLR事件包裝

(3)創(chuàng )建可以激發(fā)路由事件的方法

下面我們自定義一個(gè)WPF路由事件,我們給事件攜帶個(gè)參數,為此需要創(chuàng )建一個(gè)RoutedEventArgs類(lèi)的派生類(lèi)。如下:

using System;using System.Windows;namespace MyRoutedEvent{    //事件參數    class ReportTimeRoutedEventArgs:RoutedEventArgs    {        public ReportTimeRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }        public DateTime ClickTime { get; set; }    }}

然后,創(chuàng )建一個(gè)Button類(lèi)的派生類(lèi)并按前面的步驟為其添加路由事件:

using System;using System.Windows.Controls;using System.Windows;namespace MyRoutedEvent{    class TimeButton:Button    {        //聲明和注冊路由事件        public static readonly RoutedEvent ReportTimeRoutedEvent =            EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeRoutedEventArgs>), typeof(TimeButton));        //CLR事件包裝        public event RoutedEventHandler ReportTime        {            add { this.AddHandler(ReportTimeRoutedEvent, value); }            remove { this.RemoveHandler(ReportTimeRoutedEvent, value); }        }        //激發(fā)路由事件,借用Click事件的激發(fā)方法        protected override void OnClick()        {            base.OnClick();//保證Button原有功能正常使用,Click事件被激發(fā)            ReportTimeRoutedEventArgs args = new ReportTimeRoutedEventArgs(ReportTimeRoutedEvent, this);            args.ClickTime = DateTime.Now;            this.RaiseEvent(args);//UIElement及其派生類(lèi)                    }    }}

下面是程序界面的XAML代碼,看下如何消費這個(gè)路由事件:

<Window x:Class="MyRoutedEvent.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:MyRoutedEvent"         Title="MainWindow" Height="350" Width="525">    <Grid x:Name="grid1" local:TimeButton.ReportTime="TimeButton_ReportTime"><!---->        <Grid x:Name="grid2">            <Grid x:Name="grid3">                <StackPanel x:Name="stackPanel1">                    <ListBox x:Name="listBox1"/>                    <local:TimeButton Width="200" Height="200" Background="Aquamarine" ReportTime="TimeButton_ReportTime" /><!---->                </StackPanel>            </Grid>        </Grid>            </Grid></Window>

事件處理的后臺代碼如下:

using System.Windows;namespace MyRoutedEvent{    /// <summary>    /// MainWindow.xaml 的交互邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void TimeButton_ReportTime(object sender, ReportTimeRoutedEventArgs e)//注意參數        {            listBox1.Items.Add(e.ClickTime.ToLongTimeString()+"DebugLZQ");        }
}}

假如我如果想在后臺代碼中消費定義的路由事件,該如何做呢?

  /// <summary>    /// MainWindow.xaml 的交互邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            this.grid1.AddHandler(TimeButton.OnReportTimeRoutedEvent, new RoutedEventHandler(MainWindow_MEvent));            this.grid1.AddHandler(TimeButton.OnReportTimeRoutedEvent, new RoutedEventHandler(TimeButton_ReportTime1));//監聽(tīng)OnReportTimeRoutedEvent的路由事件                    }        private void TimeButton_ReportTime(object sender, ReportTimeRoutedEventArgs e)        {            listBox1.Items.Add(e.ClickTime.ToLongTimeString() + "DebugLZQ");        }        private void TimeButton_ReportTime1(object sender, RoutedEventArgs e)        {            ReportTimeRoutedEventArgs ss = e as ReportTimeRoutedEventArgs;//這里使用了一個(gè)轉換,父類(lèi)轉換程子類(lèi)的引用,只有轉換了,才能被AddHandler調用。            listBox1.Items.Add(ss.ClickTime.ToLongTimeString() + "DebugLZQ");        }        void MainWindow_MEvent(object sender, RoutedEventArgs e)        {            MessageBox.Show("dddd");        }    }

 

程序運行效果如下:

小結:UIElement類(lèi)是路由事件和附加事件的分水嶺,因為從UIElement類(lèi)開(kāi)始才具備了再界面上顯示的能力,也因為RaiseEvent、AddHandler和RemoveHandler這些方法也定義在UIElement類(lèi)中。附加事件也只能算是路由事件的一種用法而不是一個(gè)新的概念,其本質(zhì)還是路由事件。 

分類(lèi): WPF
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C# WPF抽屜效果實(shí)現(C# WPF Material Design UI: Navigation Drawer & PopUp Menu)
WPF之深入淺出話(huà)事件
你知道WPF與WinForms的區別嗎?
WPF快速入門(mén)系列(1)——WPF布局概覽
WPF中獲取動(dòng)態(tài)添加控件的ActualHeight和相對坐標
WPF學(xué)習筆記 - Resource
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久