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

UI添加消息聚合器

xaml 复制代码
        <md:Snackbar
            x:Name="LoginSnakeBar"
            Grid.ColumnSpan="2"
            Panel.ZIndex="1"
            MessageQueue="{md:MessageQueue}" />

注册提示消息

文件:Mytodo.Views.LoginView.cs构造函数添加内容

c# 复制代码
//注册提示消息
aggregator.ResgiterMessage(arg =>
                           {
                               LoginSnakeBar.MessageQueue.Enqueue(arg.Message);
                           }, "Login");

在需要的地方添加提示消息

修改文件:Mytodo.ViewModels.LoginViewModel.cs

c# 复制代码
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)
        {

        }
        #endregion

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


            ExecuteCommand = new DelegateCommand<string> (Execute);
            this.loginService = loginService;
            this.aggregator = aggregator;
        }
    }
}
相关推荐
努力变厉害的小超超41 分钟前
ArkTS中的组件基础、状态管理、样式处理、class语法以及界面渲染
笔记·鸿蒙
秃头佛爷42 分钟前
Python学习大纲总结及注意事项
开发语言·python·学习
△曉風殘月〆2 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
dayouziei3 小时前
java的类加载机制的学习
java·学习
aloha_7895 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
dsywws6 小时前
Linux学习笔记之vim入门
linux·笔记·学习
晨曦_子画7 小时前
3种最难学习和最容易学习的 3 种编程语言
学习
城南vision7 小时前
Docker学习—Docker核心概念总结
java·学习·docker
ctrey_8 小时前
2024-11-1 学习人工智能的Day20 openCV(2)
人工智能·opencv·学习
十年之少8 小时前
由中文乱码引来的一系列学习——Qt
学习