004 CommunityToolkit.Mvvm Hello World

99 阅读1分钟

代码地址

gitee.com/khlbat/wpf-…

安装依赖

<ItemGroup>
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="NLog" Version="5.2.8" />
    <PackageReference Include="NLog.Schema" Version="5.2.8" />
</ItemGroup>

目录结构

001.png

关键代码

App.xaml

<Application
    x:Class="mvvm_hello.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:mvvm_hello"
    d1p1:Ignorable="d"
    StartupUri="View/MainWindowView.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator
                xmlns:vm="clr-namespace:mvvm_hello.ViewModel"
                x:Key="Locator"
                d:IsDataSource="True" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

ViewModelLocator

using Microsoft.Extensions.DependencyInjection;

namespace mvvm_hello.ViewModel
{
    internal class ViewModelLocator
    {
        public static IServiceProvider ServiceProvider { get; private set; }

        public ViewModelLocator()
        {
            ServiceProvider = ConfigureServices();
        }

        private static IServiceProvider ConfigureServices()
        {
            ServiceCollection services = new();

            services.AddSingleton<MainWindowViewModel>();

            return services.BuildServiceProvider();
        }

        public MainWindowViewModel MainWindowViewModel { get => ServiceProvider.GetService<MainWindowViewModel>(); }
    }
}

MainWindowViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows.Input;

namespace mvvm_hello.ViewModel
{
    internal partial class MainWindowViewModel : ObservableObject
    {
        private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        public MainWindowViewModel()
        {
            InitProperty();
            InitCommand();
        }

        #region property

        private void InitProperty()
        {
            Msg = "Hello World";
        }

        [ObservableProperty]
        private string msg;

        [ObservableProperty]
        private string msgInput;

        #endregion

        #region command

        private void InitCommand()
        {
            SayHelloCmd = new RelayCommand(SayHelloCmdHandler);
        }

        public ICommand SayHelloCmd { get; private set; }

        private void SayHelloCmdHandler()
        {
            if (null == MsgInput || 0 == MsgInput.Length)
            {
                Msg = "Hello Mvvm";
            }
            else
            {
                Msg = "Hello " + MsgInput;
            }
            log.Info("hello mvvm");
        }

        #endregion
    }
}

运行效果

002.gif