参考
https://gitee.com/ie81/microsoft-toolkit-mvvm-template
添加依赖
<ItemGroup>
<PackageReference Include="DryIoc.dll" Version="4.8.6" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
</ItemGroup>
添加 ViewModel, View Model 文件夹
![在这里插入图片描述](https://img-blog.csdnimg.cn/c9e517b6c59147a58509663d71637376.png)
代码
MainViewModel
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
namespace my_tools.ViewModel
{
public class MainViewModel : ObservableObject
{
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public MainViewModel()
{
InitCommand();
}
private void InitCommand()
{
SayHelloCmd = new RelayCommand<string>((arg) => SayHello(arg));
}
#region 属性
private string msg;
public string Msg
{
get => msg;
set => SetProperty(ref msg, value);
}
#endregion
#region 命令
public RelayCommand<string> SayHelloCmd { get; set; }
private void SayHello(string arg)
{
Msg = "hello " + arg;
log.Info($"obj:{arg}");
}
#endregion
}
}
ViewModelLocator
using DryIoc;
namespace my_tools.ViewModel
{
public class ViewModelLocator
{
private readonly Container _container;
public ViewModelLocator()
{
_container = new Container();
_container.Register<MainViewModel>();
}
public MainViewModel Main => _container.Resolve<MainViewModel>();
}
}
App.xaml
<Application
x:Class="my_tools.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:my_tools"
d1p1:Ignorable="d"
StartupUri="View/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator
xmlns:vm="clr-namespace:my_tools.ViewModel"
x:Key="Locator"
d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window
x:Class="my_tools.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:my_tools"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="My Tools"
Width="800"
Height="450"
DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Grid>
<StackPanel Margin="10" Orientation="Vertical">
<TextBox x:Name="msg" />
<Button
Margin="0,10"
Command="{Binding SayHelloCmd}"
CommandParameter="{Binding ElementName=msg, Path=Text}"
Content="Click" />
<TextBlock FontSize="50" Text="{Binding Msg}" />
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using my_tools_core.core.consts;
using System.Windows;
namespace my_tools.View
{
public partial class MainWindow : Window
{
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
log.Info($"version:{GlobalConst.version} hello world");
}
}
}
运行效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/c82bb7f98bf441d5bd3e276688363044.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAa2hsYmF0,size_20,color_FFFFFF,t_70,g_se,x_16)