.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建立自定义事件数据类。
.net 中 C# 简单自定义事件实现的更多相关文章
- vue2.0中v-on绑定自定义事件
vue中父组件通过prop传递数据给子组件,而想要将子组件的数据传递给父组件,则可以通过自定义事件的绑定. 每个Vue实例都实现了[事件接口],即: 1.使用 $on(eventName) 监听事件 ...
- vue2.0中v-on绑定自定义事件的理解
vue中父组件通过prop传递数据给子组件,而想要将子组件的数据传递给父组件,则可以通过自定义事件的绑定. 每个Vue实例都实现了[事件接口],即: 1.使用 $on(eventName) 监听事件 ...
- C#超简单自定义事件
我知道你为啥点进来,所以不要犹豫了,立刻马上果断创建控制台项目,直接复制下面精干短小而又强大的代码运行: using System; using System.Collections.Generic; ...
- js简单自定义事件与主动触发事件
var events = { addHandler: function (element, eventType, handler) { if (element.addEventListener) { ...
- javascript高级技巧篇(作用域安全、防篡改、惰性载入、节流、自定义事件,拖放)
安全的类型检测 在任何值上调用Object原生的toString()方法,都会返回一个[object NativeConstructorName]格式字符串.每个类在内部都有一个[[Class]]属性 ...
- Spring 事件(2)- 自定义事件
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- vue第八单元(组件通信 子父,父子组件通信 自定义事件 事件修饰符 v-model props验证 )
第八单元(组件通信 子父,父子组件通信 自定义事件 事件修饰符 v-model props验证 ) #课程目标 掌握使用props让父组件给子组件传参(重点) 掌握props属性的使用以及prop验证 ...
- JavaScript使用自定义事件实现简单的模块化开发
WEB前端最常见驱动方式就是事件了, 所有交互等等都是通过事件,前端的常见事件有: UI事件: 焦点事件: 鼠标事件: 滚轮事件: 文本事件: 键盘事件: 变动事件: 现在网页上有一个输入框, 如果我 ...
- flex中dispatchEvent的用法(自定义事件) .
Evevt和EventDispatcher类在as3的事件机制中是很重要的角色,dispatchEvent()是EventDispatcher类的一个事件发送方法,它可以发送出Event类或其子类的实 ...
随机推荐
- ArcGIS API for Silverlight部署本地地图服务
这一节我们来讲新建立的ArcGIS API for Silverlight应用程序如何加载自己的地图服务的问题,网上的资料讲的都有点含糊不清,这次我们详细的讲一下配置的步骤: 首先介绍下我们的开发和部 ...
- 深入浅出javascript(四)网页运行原理
这一篇是根据不同的书本知识归纳的内容,解答的问题是浏览器是如何工作的? 另外,还有一些长篇的内容,写的是浏览器内部如何架构的,这些内容非常复杂艰深,比现在所写的内容又低了一个层级,希望有时间能总结贴出 ...
- winSockets编程(七)WSAAsyncSelect模式
占位## #include <WinSock2.h> #include <Windows.h> #include <StrSafe.h> #pragma comme ...
- 改Android手机定位位置
手机定位方法 1,gps等卫星定位,如美国的gps,欧洲的伽利略,中国的北斗等,通过至少三颗卫星,用三角定位和时间等算法,计算出设备的经纬度,到地图上找到这个经纬度的地名 2,移动运营商基站定位,通过 ...
- (转) HighCharts 非规律日期 多条曲线的 绘画
转自:http://blog.csdn.net/z69183787/article/details/8651296 项目中需要为A,B 2个元素 绘出统计值的曲线,但A与B 的 时间点 并不一致,查找 ...
- spring之jdbcTemplate
spring的另一个功能模块data access对于数据库的支持 spring data access第一个helloword案例: 使用java程序实现访问配置 1.导包 2.测试案例 @Test ...
- Qt_HelloWrold
新建工程 -> 选择Qt Gui 应用 然后点击选择 在弹出的对话框中填写名称,创建路径等信息: 点击下一步,选择该工程的编译器. 点击下一步,可以选择生成的主窗口文件.不过这里我们仅仅用简单的 ...
- spfa负环判断
正常spfa中加入time数组,循环判断一个点是否入队并更新了n次以上注意是 > n!!其余的没有什么问题 扩展的还有,寻找所有负环上的点,这个可以在spfa中time 发现负环的时候,对那个点 ...
- 数据统计--union all 执行多条sql
需求--统计hive某张表type字段不同取值的数据量 我们已知某张表的type的取值是1,2,3,4,5,想要统计不同type的数据量,并清晰的展现出来.可以通过union all 的方式,sql如 ...
- IT项目管理流程以及每个步骤用到的文档
IT项目管理从大的方面可分为:1)项目启动阶段:2)项目计划阶段:3)项目的实施阶段:4)项目的结项阶段 1)项目启动阶段: 1.项目启动流程规范: 1.1项目启动的简介.目的和范围 1.2目的可行性 ...