-
Notifications
You must be signed in to change notification settings - Fork 382
Broadcast广播器
Dream.Machine edited this page May 3, 2019
·
12 revisions
广播主要是解决C/S(Winform和WPF都可以)中不同Window,Page,UserControl,ViewModel之间的消息通知。
你可以在任何类中注册广播,也可以在任何类中推送这个广播,注册接收器的地方都会收到广播。
在App.cs中 重写 OnStartup
protected override void OnStartup(StartupEventArgs e)
{
//初始化Broadcast
Broadcast.Initialize();
}
//注册广播通知
Broadcast.RegisterBroadcast(Type,()=>
{
System.Console.WriteLine($"收到广播");
});
//注册广播通知-带参数
Broadcast.RegisterBroadcast<string>(Type,(data)=>
{
System.Console.WriteLine($"收到广播:{data}");
});
//推送广播
Broadcast.PushBroadcast(Type);
//推送广播-带参数
Broadcast.PushBroadcast(Type, "Hello world");
//卸载广播接收器
Broadcast.UninstallBroadcast(Type);
//注册广播接收器
Broadcast.RegisterBroadcast<Action>("T", (action) =>
{
action();
});
//推送广播
Broadcast.PushBroadcast<Action>("T", () =>
{
//需要推送的函数
MessageBox.Show("Test");
});
//推送广播
Broadcast.PushBroadcast<string,int>(Type, "给我你们的ID?",(value)=>
{
System.Console.WriteLine($"收到订阅者消息:{value}");
});
//注册广播接收器X,当前接收器的ID为X
Broadcast.RegisterBroadcast<string, int>(Type, (data, callBack) =>
{
System.Console.WriteLine($"收到广播:{data}");
//回发消息,数据类型为int
callBack(10000);
});