c# - MVVM WPF - 登录逻辑

标签 c# wpf mvvm authentication

我需要有关应用中身份验证属性的帮助。

enter image description here

Q1:我对一些模型的 JSON 进行了反序列化,因此在 ViewModel 中我有一个具有 idUser(来自服务器的响应)的模型。如何在不破坏 MVVM 架构的情况下在 FriendsViewModel 中使用该信息?

最好的问候, 安东尼

最佳答案

执行流程可能是这样的:

  • 应用程序 View 模型启动(您的图表缺少此 View 模型);
  • 它使用登录 View 模型请求异步登录;
  • 如果登录成功,则应用 View 模型创建通用 View 模型;
  • 普通 View 模型加载 friend
  • 如果登录失败,应用程序 View 模型会创建错误 View 模型。

示例代码(您可能想使用自己喜欢的 IoC 来创建 View 模型和注入(inject)服务):

public class ApplicationVm
{
    private readonly IInteractionService interactionService;
    private readonly IDataService dataService;

    private async Task LoadDataAsync()
    {
        try
        {
            // we need to ask for credentials
            var loginVm = new LoginVm();
            if (interactionService.ShowInDialog(loginVm) == true)
            {
                // performing login
                var userId = await dataService.LoginAsync(loginVm.UserName, loginVm.Password);
                // setting content to general view model, which is the payload of our app
                Content = new GeneralVm(userId, dataService);
            }
            else
            {
                // setting content to stub, which shows us "Login cancelled" message
                Content = new StubVm { Message = "Login cancelled" };
            }
        }
        catch (Exception ex)
        {
            // setting content to stub, which shows us "Login failed" message
            Content = new StubVm { Message = $"Login failed: {ex.Message}" };
        }
    }

    public ApplicationVm(IInteractionService interactionService, IDataService dataService)
    {
        this.interactionService = interactionService;
        this.dataService = dataService;
        var _ = LoadDataAsync();
    }

    public object Content { get; private set; }
}

public interface IInteractionService
{
    bool? ShowInDialog(object viewModel);
}

public interface IDataService
{
    Task<int> LoginAsync(string userName, string password);
    Task<IEnumerable<Friend>> GetFriendsAsync(int userId);
}

public class LoginVm
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

public class GeneralVm
{
    private readonly int userId;
    private readonly IDataService dataService;

    private async Task LoadFriendsAsync()
    {
        var friends = await dataService.GetFriendsAsync(userId);
        Friends = friends
            .Select(model => new FriendVm(userId, model));
    }

    public GeneralVm(int userId, IDataService dataService)
    {
        this.userId = userId;
        this.dataService = dataService;
    }

    public IEnumerable<FriendVm> Friends { get; private set; }
}

public class FriendVm
{
    public FriendVm(int userId, Friend model)
    {
    }
}

public class Friend { }

public class StubVm
{
    public string Message { get; set; }
}

关于c# - MVVM WPF - 登录逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600535/

相关文章:

C# 缓存 txt 文件或使用 File.ReadLines

wpf - 小按钮wpf内的图标

wpf - WPF 的 MVVM 模式 - 再次绘制 2d 图

c# - 如何通过 ViewModel 控制 View VisualState

wpf - 一个组合框,提供可调整大小的文件名完成列表和带有历史记录的下拉列表

c# - WPF:ViewModel 类型不包含任何可访问的构造函数

c# - 从其他属性的代码中测试属性

c# - 错误: C# Attempted to read or write protected memory

c# - Debug模式与 Release模式 - IsJITOptimizerDisabled 属性

WPF Datepicker 只能选择日期列表