Skip to content

Commit

Permalink
X
Browse files Browse the repository at this point in the history
  • Loading branch information
944095635 committed Apr 12, 2019
1 parent 3239421 commit 1718597
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 56 deletions.
36 changes: 0 additions & 36 deletions DMSkin.Core/Common/Broadcast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,6 @@ public static bool RegisterBroadcast<T, T1>(string name, Action<T, Action<T1>> a
{
return Register(name, action);
}

/// <summary>
/// 注册广播接收器-广播返回值
/// </summary>
/// <typeparam name="T">广播传递的数据类型</typeparam>
/// <typeparam name="T1">广播返回的数据类型</typeparam>
/// <param name="name">广播名称</param>
/// <param name="action">广播的回调函数</param>
public static bool RegisterBroadcast<T, T1>(string name, Func<T, T1> action)
{
return Register(name, action);
}
#endregion

#region 推送广播
Expand Down Expand Up @@ -136,30 +124,6 @@ public static void PushBroadcast<T, T1>(string name, T parameter = default, Acti
}
}
}

/// <summary>
/// 推送广播-并执行回调
/// </summary>
/// <typeparam name="T">广播传递的数据类型</typeparam>
/// <typeparam name="T1">广播回调消息的数据类型</typeparam>
/// <param name="name">广播名称</param>
/// <param name="parameter">广播传递的数据</param>
/// <param name="callBack">广播订阅者的回传信息</param>
public static T1 PushBroadcast<T, T1>(string name, T parameter = default)
{
var broadcast = FindBroadcast(name);
if (broadcast != null)
{
foreach (var item in broadcast)
{
if (item is Func<T, T1> action)
{
return action.Invoke(parameter);
}
}
}
return default;
}
#endregion

#region 卸载广播
Expand Down
29 changes: 14 additions & 15 deletions Demos/DMSkinDemo/View/PageBroadcast.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel>
<GroupBox HorizontalAlignment="Left" Header="单向通知广播(适用于单向广播通知)">
<GroupBox HorizontalAlignment="Left" Header="单向通知广播(适用于单向推送消息或数据)">
<UniformGrid HorizontalAlignment="Left" Rows="1">
<Button
Margin="10"
Expand All @@ -34,7 +34,19 @@
Content="推送单向广播" />
</UniformGrid>
</GroupBox>
<GroupBox HorizontalAlignment="Left" Header="多播广播(发布广播,并发布回传通道,适用于多个数据回传)">
<GroupBox HorizontalAlignment="Left" Header="单向广播(发布广播,参数为Action,适用推送函数)">
<UniformGrid HorizontalAlignment="Left" Rows="1">
<Button
Margin="10"
Command="{Binding RegisterBroadcast3}"
Content="注册广播接收器-推送" />
<Button
Margin="10"
Command="{Binding PushBroadcast3}"
Content="推送广播并推送函数" />
</UniformGrid>
</GroupBox>
<GroupBox HorizontalAlignment="Left" Header="回调广播(发布广播,并发布回传通道,适用于数据回传)">
<UniformGrid HorizontalAlignment="Left" Rows="1">
<Button
Margin="10"
Expand All @@ -50,19 +62,6 @@
Content="推送广播接受回调" />
</UniformGrid>
</GroupBox>
<GroupBox HorizontalAlignment="Left" Header="直连广播(发布广播,并接受回传信息,适用于单个数据回传)">
<UniformGrid HorizontalAlignment="Left" Rows="1">
<Button
Margin="10"
Command="{Binding RegisterBroadcast3}"
Content="注册广播接收器-返回值" />
<Button
Margin="10"
Command="{Binding PushBroadcast3}"
Content="推送广播接受返回值" />
</UniformGrid>
</GroupBox>

<GroupBox HorizontalAlignment="Left" Header="卸载所有广播">
<Button
Margin="10"
Expand Down
16 changes: 11 additions & 5 deletions Demos/DMSkinDemo/ViewModel/BroadcastViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using DMSkin.Core;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace DMSkinDemo.ViewModel
Expand Down Expand Up @@ -112,20 +114,24 @@ public ObservableCollection<string> Message
/// </summary>
public ICommand RegisterBroadcast3 => new DelegateCommand(obj =>
{
Broadcast.RegisterBroadcast<string, string>("test1", (e) =>
//注册广播接收器
Broadcast.RegisterBroadcast<Action>("test1", (action) =>
{
Message.Add($"接收到来直连广播的消息:{e}");
return "我收到广播了.";
action();
});
Message.Add("注册直连广播接收器成功!");
});

/// <summary>
/// 推送返回回调
/// </summary>
public ICommand PushBroadcast3 => new DelegateCommand(obj =>
{
Message.Add($"推送广播并接受返回消息:{Broadcast.PushBroadcast<string, string>("test1", "你好,这是一条直连通知广播。")}");
//推送广播
Broadcast.PushBroadcast<Action>("test1", () =>
{
//需要推送的函数
MessageBox.Show("Test");
});
});


Expand Down

0 comments on commit 1718597

Please sign in to comment.