增加全局账户名同步
增加静态变量
添加文件: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绑定
- 增加命名空间
xmlns:model="clr-namespace:Mytodo.Common.Models"
- 修改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

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

被折叠的 条评论
为什么被折叠?



