WPF实战学习笔记31-登录界面全局通知

UI添加消息聚合器

        

注册提示消息

文件: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 定义命令
        /// 
        /// 执行登录|推出等相关命令
        /// 
        public DelegateCommand ExecuteCommand { get; set; }
        #endregion

        #region 定义属性

        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }

        /// 
        /// 注册区域字段
        /// 
        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 命令方法
        /// 
        /// ExecuteCommand对应的方法
        /// 
        /// 
        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 RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }
        #endregion

        public LoginViewModel(ILoginService loginService, IEventAggregator aggregator)
        {
            //初始化对象
            RUserDto=new ResgiterUserDto();


            ExecuteCommand = new DelegateCommand (Execute);
            this.loginService = loginService;
            this.aggregator = aggregator;
        }
    }
}

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)