UI添加消息聚合器
<md:Snackbarx:Name="LoginSnakeBar"Grid.ColumnSpan="2"Panel.ZIndex="1"MessageQueue="{md:MessageQueue}" />
注册提示消息
文件:Mytodo.Views.LoginView.cs构造函数添加内容
//注册提示消息
aggregator.ResgiterMessage(arg =>{LoginSnakeBar.MessageQueue.Enqueue(arg.Message);}, "Login");
在需要的地方添加提示消息
修改文件:Mytodo.ViewModels.LoginViewModel.cs
using AutoMapper;
using Mytodo.Extensions;
using Mytodo.Service;
using MyToDo.Api.Context;
using MyToDo.Share;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;namespace Mytodo.ViewModels
{public class LoginViewModel : BindableBase, IDialogAware{#region 定义命令/// <summary>/// 执行登录|推出等相关命令/// </summary>public DelegateCommand<string> ExecuteCommand { get; set; }#endregion#region 定义属性public int SelectIndex{get { return selectIndex; }set { selectIndex = value; RaisePropertyChanged(); }}/// <summary>/// 注册区域字段/// </summary>public ResgiterUserDto RUserDto{get { return rUserDto; }set { rUserDto = value; RaisePropertyChanged(); }}public string Password{get { return password; }set { password = value; }}public string Account{get { return username; }set { username = value; }}#endregion#region 定义重要字段#endregion#region 定义普通字段private int selectIndex;private ResgiterUserDto rUserDto;private string password;private string username;private readonly ILoginService loginService;private readonly IEventAggregator aggregator;#endregion#region 命令方法/// <summary>/// ExecuteCommand对应的方法/// </summary>/// <param name="obj"></param>private void Execute(string obj){switch (obj){case "Login": Login(); break;case "LoginOut": LoginOut(); break;case "Resgiter": Resgiter(); break;case "ResgiterPage": SelectIndex = 1; break;case "Return": SelectIndex = 0; break;}}async private void Resgiter(){if (string.IsNullOrWhiteSpace(RUserDto.Account) ||string.IsNullOrWhiteSpace(RUserDto.UserName) ||string.IsNullOrWhiteSpace(RUserDto.PassWord) ||string.IsNullOrWhiteSpace(RUserDto.NewPassWord)){aggregator.SendMessage("请输入完整的注册信息!", "Login");return;}if (RUserDto.PassWord != RUserDto.NewPassWord){aggregator.SendMessage("密码不一致,请重新输入!", "Login");return;}var resgiterResult = await loginService.Resgiter(new UserDto(){Account = RUserDto.Account,UserName = RUserDto.UserName,PassWord = RUserDto.PassWord});if (resgiterResult != null && resgiterResult.Status){//aggregator.SendMessage("注册成功", "Login");//注册成功,返回登录页页面SelectIndex = 0;}// else//aggregator.SendMessage(resgiterResult.Message, "Login");}async private void LoginOut(){if (string.IsNullOrWhiteSpace(Account) ||string.IsNullOrWhiteSpace(Password)){aggregator.SendMessage("Account或Password", "Login");return;}var loginResult = await loginService.Login(new UserDto(){Account = Account,PassWord = Password});if (loginResult != null && loginResult.Status){RequestClose?.Invoke(new DialogResult(ButtonResult.OK));}else{//登录失败提示...aggregator.SendMessage(loginResult.Message, "Login");}}async private void Login(){if (string.IsNullOrWhiteSpace(Account) ||string.IsNullOrWhiteSpace(Password)){aggregator.SendMessage("密码或账户为空,请重新输入", "Login");return;}var loginResult = await loginService.Login(new UserDto(){Account = Account,PassWord = Password});if (loginResult != null && loginResult.Status){RequestClose?.Invoke(new DialogResult(ButtonResult.OK));}else{//登录失败提示...aggregator.SendMessage(loginResult.Message, "Login");}}#endregion#region 启动项#endregion#region 继承public string Title { get; set; } = "Todo";public event Action<IDialogResult> RequestClose;public bool CanCloseDialog(){return true;}public void OnDialogClosed(){LoginOut();}public void OnDialogOpened(IDialogParameters parameters){}#endregionpublic LoginViewModel(ILoginService loginService, IEventAggregator aggregator){//初始化对象RUserDto=new ResgiterUserDto();ExecuteCommand = new DelegateCommand<string> (Execute);this.loginService = loginService;this.aggregator = aggregator;}}
}