当前位置:网站首页>【wpf】自定义事件总结(Action, EventHandler)
【wpf】自定义事件总结(Action, EventHandler)
2022-08-10 00:54:00 【code bean】
Action和EventHandle
Action和EventHandler都是.net帮我定义好的委托,我们可以直接使用,方便的定义事件。
利用Action定义事件
//无参事件
event Action Event1;
//有参事件
event Action<int, int, string> Event2;
事件订阅和事件处理
public 事件测试()
{
InitializeComponent();
// 事件的订阅
Event1 += 事件测试_Event1;
Event2 += 事件测试_Event2;
}
private void 事件测试_Event1()
{
MessageBox.Show("事件测试_Event1");
}
private void 事件测试_Event2(int arg1, int arg2, string arg3)
{
MessageBox.Show($"{arg1 + arg2} {arg3}");
}
事件的触发
private void btn1_Click(object sender, RoutedEventArgs e)
{
//事件的触发
Event1?.Invoke();
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
//事件的触发
Event2?.Invoke(1, 2, "test");
}
这里通过Invoke调用和直接调用,效果和效率都是一样的,这样写凸显出这是这个事件的触发,事件后的问号表示,如果事件为空就不触发的一种简单写法。
EventHandler
EventHandler,在.net6中感觉和Action的区别已经不大了,调用时有些差别(见下面代码)。之前的版本中只有用EventHandler定义的事件才能在Xaml中使用,在.net6使用Action定义的事件也可以在Xaml中使用了。
这里我定义了一个控件,并分别通过两种方式定义了事件
public class Class1 : Button
{
public event Action EventTest;
public event EventHandler<string> Event3;
protected override void OnClick()
{
EventTest?.Invoke();
Event3?.Invoke(this, "123"); // 事件的调用方式稍有不同
base.OnClick();
}
}
发现在前台Xaml都是可以直接使用的:
<local:Class1 EventTest="Class1_EventTest" Event3="Class1_Event3" >Class1</local:Class1>
private void Class1_EventTest()
{
MessageBox.Show("Class1_EventTest");
}
private void Class1_Event3(object sender, string e)
{
MessageBox.Show($"Class1_Event3: {e}");
}
小结:
可以说通过Action和EventHandle,自定义事件是相当的方便简单了。
边栏推荐
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 数据建模已死,真的吗?
- 信息化和数字化的核心差异
- oracle的数据导入导出
- 无js实现弹出层效果
- Pyscript,创建一个能执行crud操作的网页应用
- unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
- Unity editor extension interface uses List
- Initial attempt at UI traversal
- Interlay集成至Moonbeam,为网络带来interBTC和INTR
猜你喜欢
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
How to turn off system protection in Win11?How to turn off the system protection restore function?
unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
Aptos 深度解读:机遇、挑战与风险
Solidity最强对手:MOVE语言及新公链崛起
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
Not, even the volume of the king to write code in the company are copying and pasting it reasonable?
随机推荐
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
破产企业的职工退休怎么办?
罗彻斯特大学 | 现在是什么序列?蛋白质序列的贝叶斯优化的预训练集成
Visual low-code system practice based on design draft identification
Minimum number of steps to get out of the maze 2
对修饰器的实验支持功能在将来的版本中可能更改。在 “tsconfig“ 或 “jsconfig“ 中设置 “experimentalDecorators“ 选项以删除此警告
3438. 数制转换
DP 优化方法合集
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy
删除表空间数据文件
什么是 PWA
In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?
【软考软件评测师】软件测试基础知识
Shader Graph learns various special effects cases
彩色袜子题
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
Data storage - the C language
跳房子游戏