xaml;

cs文件

public partial class MainWindow

{ public MainWindow()

{

InitializeComponent();

DataContext = App.Current.Services.GetService();

}

}

VM:

partial class MainWindowViewModel : BindableBase

{

[RelayCommand]

private void Close()

{

Logger.Info("000000000000000000000000000");

// 在这里添加关闭窗口时需要执行的逻辑

MessageBox.Show("窗口正在关闭");

}

}

第二种方式:委托 Vm:

public ICommand CloseCommand => _closeCommand ?? (_closeCommand = new RelayCommand(CloseExecute));

private void CloseExecute()

{

Logger.Info("000000000000000000000000000");

// 在这里添加关闭窗口时需要执行的逻辑

MessageBox.Show("窗口正在关闭");

}

登录: view

Margin="0,50,0,0"

HorizontalAlignment="Center"

FontSize="25"

FontWeight="Bold"

Style="{StaticResource TextBlockDefaultPrimary}"

Text="哈哈" />

Grid.Row="0"

Grid.Column="0"

Width="250"

Margin="0,50,0,0"

hc:InfoElement.Title="工号"

hc:InfoElement.TitlePlacement="Left"

hc:InfoElement.TitleWidth="50"

Text="{Binding Username, UpdateSourceTrigger=PropertyChanged}" />

Margin="0,15,0,0" VerticalAlignment="Bottom" hc:TitleElement.TitleWidth="50" Width="250" Header="密码" Style="{StaticResource GroupBoxOriginal}" HorizontalContentAlignment="Left" hc:TitleElement.TitlePlacement="Left">

Margin="0,10,0,0"

HorizontalAlignment="Center"

FontSize="12"

FontWeight="Medium"

Foreground="#D7596D"

Text="{Binding ErrorMessage,NotifyOnTargetUpdated=True}"

TextWrapping="Wrap" />

x:Name="btnLogin"

Margin="20,0,20,0"

Command="{Binding LoginCommand}"

Width="350"

Content="立即登录"

Cursor="Hand"

IsDefault="True"

Style="{DynamicResource ButtonPrimary}" />

对应cs文件:

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using DetectAOI.Core;

using DetectAOI.ViewModels;

using Microsoft.Extensions.DependencyInjection;

namespace DetectAOI.Views

{

public partial class LoginView : Page

{

public LoginView()

{

DataContext = App.Current.Services.GetService();

InitializeComponent();

}

}

}

VM:

using System;

using DetectAOI.Common;

using DetectAOI.Core.Api;

using DetectAOI.Core;

using CommunityToolkit.Mvvm.ComponentModel;

using CommunityToolkit.Mvvm.Input;

using System.Threading.Tasks;

using CommunityToolkit.Mvvm.Messaging;

using DetectAOI.Core.Api.Models;

using System.Threading;

using System.Linq;

using HandyControl.Tools.Extension;

using System.Windows.Threading;

using DetectAOI.DialogViews;

using HandyControl.Controls;

namespace DetectAOI.ViewModels

{

public partial class LoginViewModel:BindableBase

{

//这两个也就属于m的数据绑定

[ObservableProperty]

private string _username = "leader";

[ObservableProperty]

private string _password = "123456";

[ObservableProperty]

private string _errorMessage;

private IHttpService _httpService;

public LoginViewModel(IHttpService httpService)

{

_httpService = httpService;

}

//命令绑定 名称必须一致

[RelayCommand]

private async Task Login()

{

if (StrUtil.IsEmpty(Username))

{

ErrorMessage = "工号不能为空";

return;

}

if (StrUtil.IsEmpty(Password))

{

ErrorMessage = "密码不能为空";

return;

}

ErrorMessage = "";

var loginResult = await _httpService.Login(new Core.Api.Models.LoginBodyBo() { username = Username, password = Password });

if (!loginResult.Success)

{

Logger.Error(loginResult.Msg);

ErrorMessage = loginResult.Msg;

return;

}

Session.LogingUsername = Username;

Session.Token = loginResult.Data.Token;

//获取权限信息

AjaxResult ajaxResult = await _httpService.UserInfo();

UserInfoVo userInfoVo = ajaxResult.Data;

//登录事件

WeakReferenceMessenger.Default.Send(new LoginEvent(userInfoVo));

}

}

}

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: