.net 中 C# 简单自定义事件实现
个人认为事件处理机制是一种很好的机制
特别是可以方便安全的实现窗口间(子窗口对父窗口,子窗口间等)的消息传递、功能调用
下面展现的源自以前论坛上看到的一套方法,可能记得不大准确,所以可能不规范,我的理解和注释也很可能有谬误
但在实际操作中能满足需求也没有发现异常。
以实现子窗口引起父窗口控件变化为例
父窗口 XAML段
1 <Window x:Class="EventTransBetweenFroms.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <Label Content="我是主窗口" Height="62" HorizontalAlignment="Left" Margin="151,37,0,0" Name="label1" VerticalAlignment="Top" FontSize="36" Width="201" /> 7 <Label Content="事件已处理" Height="55" HorizontalAlignment="Left" Margin="169,116,0,0" Name="labelMessage" VerticalAlignment="Top" Foreground="Red" FontSize="28" Width="156" Visibility="Hidden" /> 8 <Button Content="子窗口" Height="66" HorizontalAlignment="Left" Margin="213,205,0,0" Name="btnCallChildWnd" VerticalAlignment="Top" Width="70" FontSize="18" Click="btnCallChildWnd_Click" /> 9 </Grid> 10 </Window>
子窗口XAML段
1 <Window x:Class="EventTransBetweenFroms.wndChild" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="wndChild" Height="300" Width="300"> 5 <Grid> 6 <Label Content="我是子窗口" FontSize="36" Height="62" HorizontalAlignment="Left" Margin="43,27,0,0" Name="label1" VerticalAlignment="Top" Width="201" /> 7 <Button Content="触发事件" Height="61" HorizontalAlignment="Left" Margin="97,111,0,0" Name="btnTriggerEvent" VerticalAlignment="Top" Width="89" FontSize="20" Click="btnTriggerEvent_Click" /> 8 </Grid> 9 </Window>
这一部分没什么特别的
现希望子窗口中的点击btnTriggerEvent按钮使父窗口中的labelMessage可见
父窗口代码段
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace EventTransBetweenFroms 16 { 17 /// <summary> 18 /// MainWindow.xaml 的交互逻辑 19 /// </summary> 20 public partial class MainWindow : Window 21 { 22 public MainWindow() 23 { 24 InitializeComponent(); 25 } 26 27 private void btnCallChildWnd_Click(object sender, RoutedEventArgs e) 28 { 29 wndChild wndchild = new wndChild(); 30 wndchild.MyEvent+=new EventHandler(HandleEvent); //添加事件托管,可以理解为注册这个事件,之后才能被响应
//EvntHandler中的参数为响应的函数名 31 wndchild.Show(); 32 } 33 private void HandleEvent(object sender, EventArgs e) 34 { 35 labelMessage.Visibility = Visibility.Visible; //响应函数的参数必须为(object sender,EventArgs e)的格式
36 } 37 } 38 }
子窗口代码段
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Shapes; 13 14 namespace EventTransBetweenFroms 15 { 16 /// <summary> 17 /// wndChild.xaml 的交互逻辑 18 /// </summary> 19 public partial class wndChild : Window 20 { 21 public wndChild() 22 { 23 InitializeComponent(); 24 } 25 26 public event EventHandler MyEvent; //声明事件 27 protected virtual void OnMyEvent(object sender, EventArgs e) //声明触发事件函数,一般以On+事件名为名 28 { 29 if (e != null) 30 { 31 MyEvent(sender, e); 32 } 33 } 34 35 private void btnTriggerEvent_Click(object sender, RoutedEventArgs e) 36 { 37 OnMyEvent(this, new EventArgs()); //调用触发事件函数,注意指明sender并建立新的EventArgs
38 } 39 } 40 }
实现。
小结:本例中自定义事件实现了期望的功能且不破坏窗口的封装,对于一些比较复杂消息传递、功能调用也可以通过继承EventArgs建立自定义事件数据类。