新建消息事件
添加文件:Mytodo.Common.Events.MessageModel.cs
c#
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Diagnostics;
namespace Mytodo.Common.Events
{
public class MessageModel
{
public string Filter { get; set; }
public string Message { get; set; }
}
public class MessageEvent:PubSubEvent<MessageModel>
{
}
}
注册、发送提示消息
c#
/// <summary>
/// 注册提示消息
/// </summary>
/// <param name="aggregator"></param>
/// <param name="action"></param>
public static void ResgiterMessage(this IEventAggregator aggregator,
Action<MessageModel> action, string filterName = "Main")
{
aggregator.GetEvent<MessageEvent>().Subscribe(action,
ThreadOption.PublisherThread, true, (m) =>
{
return m.Filter.Equals(filterName);
});
}
/// <summary>
/// 发送提示消息
/// </summary>
/// <param name="aggregator"></param>
/// <param name="message"></param>
public static void SendMessage(this IEventAggregator aggregator, string message, string filterName = "Main")
{
aggregator.GetEvent<MessageEvent>().Publish(new MessageModel()
{
Filter = filterName,
Message = message,
});
}
UI增加Snackbar
修改文件:Mytodo.Views.MainView.xaml
xaml
</materialDesign:DrawerHost.LeftDrawerContent>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<materialDesign:Snackbar
x:Name="skbar"
Panel.ZIndex="1"
MessageQueue="{materialDesign:MessageQueue}" />
<materialDesign:ColorZone
注册消息
修改文件:Mytodo.Views.MainViewcs 构造函数添加
要注意的是,我们要发送的是文本,所以,this.skbar.MessageQueue.Enqueue函数内发送的是文本。
c#
aggregator.ResgiterMessage(arg =>
{
this.skbar.MessageQueue.Enqueue(arg.Message);
});
在需要的地方推送消息
c#
aggregator.SendMessage("已完成!");