WPF实战学习笔记32-登录、注册服务添加

文章详细描述了如何在应用程序中增加全局账户名同步,通过添加静态变量和事件来实现。修改MainView的绑定,增加命名空间,并在LoginViewModel中设置账户名。同时,添加了注销功能,包括注销方法和命令。在MainViewModel中初始化注销命令,并在XAML中增加注销按钮。此外,还修复了HttpRestClient服务接口中的ExecuteAsync函数,以处理登录和注销的网络请求。

增加全局账户名同步

增加静态变量

添加文件:Mytodo.Common.Models.AppSession.cs

ausing Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Mytodo.Common.Models
{
    public class AppSession
    {

        private static String userName;

        /// <summary>
        /// A static property which you'd like to bind to
        /// </summary>
        public static String UserName
        {
            get
            {
                return userName;
            }

            set
            {
                userName = value;

                // Raise a change event
                OnUserNameChanged(EventArgs.Empty);
            }
        }

        // Declare a static event representing changes to your static property
        public static event EventHandler UserNameChanged;

        // Raise the change event through this static method
        protected static void OnUserNameChanged(EventArgs e)
        {
            EventHandler handler = UserNameChanged;

            if (handler != null)
            {
                handler(null, e);
            }
        }

        static AppSession()
        {
            // Set up an empty event handler
            UserNameChanged += (sender, e) => { return; };
        }


    }
}

修改MainView绑定
  1. 增加命名空间
xmlns:model="clr-namespace:Mytodo.Common.Models"
  1. 修改textblock绑定
<TextBlock
           Margin="0,10"
           HorizontalAlignment="Center"
           Text="{Binding Path=(model:AppSession.UserName)}" />
给账户名赋值

修改Mytodo.ViewModels.LoginViewModel.cs

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)
    {
        //UserDto myuser= new UserDto() { UserName=loginResult.Result }
        // AppSession.UserName

        AppSess
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值