-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathForm1.cs
More file actions
39 lines (34 loc) · 1.07 KB
/
Form1.cs
File metadata and controls
39 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using CodeWF.EventBus;
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1_4_8
{
public class TimeCommand : Command
{
public int Id { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 无 IOC 场景下,窗体自己订阅自己的事件处理方法。
EventBus.Default.Subscribe(this);
var btn = new Button();
btn.Text = "publish";
btn.Click += (s, e) => EventBus.Default.Publish(new TimeCommand() { Id = DateTime.Now.Millisecond });
this.Controls.Add(btn);
}
[EventHandler]
private void ReceiveCommand(TimeCommand command)
{
MessageBox.Show($"收到ID:{command.Id}");
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
// 窗体关闭时主动取消订阅,避免重复打开窗体后处理器累积。
EventBus.Default.Unsubscribe(this);
base.OnFormClosed(e);
}
}
}