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







