SoFunction
Updated on 2025-05-14

How to use MVVM toolkit in WPF

MVVM toolkit in WPF

, also known as the MVVM toolkit, is part of the Microsoft Community Toolkit. It is a lightweight but powerful MVVM (Model-View-ViewModel) library designed to help developers implement MVVM design patterns more easily.

Features

  • Independent of platform and runtime: Supports .NET Standard 2.0, .NET Standard 2.1 and .NET 6. It has nothing to do with the UI framework and can be used on UWP, WinForms, WPF, Xamarin, Uno and other platforms.
  • Easy to select and use: There is no strict requirement for application structure or coding examples, and can be used flexibly.
  • On-demand access: Freely select the components to use, all types are loosely coupled.
  • Quotation implementation: Provides implementations for interfaces contained in the base class library, but lacks the specific types required to directly use them.

usage

  • Install: In Visual Studio, search and install via NuGet Package ManagerBag,
  • Quote: Add in the projectusing ;to use the new API.
  • Implement MVVM: Through inheritanceObservableObjectTo create a ViewModel, useSetPropertyMethod implementation attribute change notification and useRelayCommandorAsyncRelayCommandto implement command mode.

Function

This library provides some basic classes and interface implementations, mainly including:

  • ObservableObjectandObservableRecipient: These classes have been implementedINotifyPropertyChangedInterface and providesSetPropertyMethod, can be triggered when the property value changesPropertyChangedEvents, simplify data binding.
  • ICommandThe implementation of the interface, such asRelayCommandandAsyncRelayCommand: These classes can help create commands, which are an important part of the MVVM pattern.
  • IMessengerand related messaging mechanisms, such asWeakReferenceMessengerandStrongReferenceMessenger, for loosely coupled communication between components.

The types contained are mainly divided into the following parts

  • ObservableObject: It has been realizedINotifyPropertyChangedInterface to create a ViewModel that responds to property changes.
  • ObservableRecipient: Base class for messaging.
  • ObservableValidator: Base class for data verification.

  • Ioc: Provides containers and related services for dependency injection.

  • RelayCommand: A simple command implementation for executing a parameterless method.
  • RelayCommand<T>: A generic versionRelayCommand, used to execute the parameter-with-parameter method.
  • AsyncRelayCommand: An asynchronous command implementation used to execute asynchronous parameterless methods.
  • AsyncRelayCommand<T>: An asynchronous command implementation used to execute asynchronous method with parameters.
  • IRelayCommandRelayCommandinterface definition.
  • IRelayCommand<T>RelayCommand<T>interface definition.
  • IAsyncRelayCommandAsyncRelayCommandinterface definition.
  • IAsyncRelayCommand<T>AsyncRelayCommand<T>interface definition.

  • IMessenger: Message delivery interface, which defines the methods of message sending and subscribing.
  • WeakReferenceMessenger: A message passing class that uses weak references to avoid memory leaks.
  • StrongReferenceMessenger: A messaging class that uses strong references.
  • IRecipient<TMessage>: Message receiver interface, used to receive specific types of messages.
  • MessageHandler<TRecipient, TMessage>: Message handler, used to process specific types of messages.

  • PropertyChangedMessage<T>: Message used for property changes.
  • RequestMessage<T>: Message used to request a specific type of response.
  • AsyncRequestMessage<T>: Asynchronous versionRequestMessage<T>
  • CollectionRequestMessage<T>: Request message for collection operations.
  • AsyncCollectionRequestMessage<T>: Asynchronous versionCollectionRequestMessage<T>
  • ValueChangedMessage<T>: Message for value changes.

Some key attributes and functions are also provided

ObservableObject

ObservableObjectyesA basic class in the library that implementsINotifyPropertyChangedinterface. This interface is part of the .NET data binding infrastructure, and when an attribute of an object changes, it notifies any element bound to that attribute.

  • usage: ViewModel is usually inherited fromObservableObjectkind.
  • Sample code
public class MyViewModel : ObservableObject
{
    private string _myProperty;
    public string MyProperty
    {
        get => _myProperty;
        set => SetProperty(ref _myProperty, value);
    }
}

[ObservableProperty] Properties

[ObservableProperty]is an Attribute that automatically generates getters and setters for observable properties, which will be triggered when the value changes.PropertyChangedevent.

  • usage: Use directly on the ViewModel property[ObservableProperty]characteristic.
  • Sample code
public partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    private string? _name;
}

This will generate an observable property similar to the following:

public string? Name
{
    get => _name;
    set => SetProperty(ref _name, value);
}

[RelayCommand] attribute

[RelayCommand]is a feature used to create commands. Commands are an important part of MVVM mode, a method used to bind the behavior of a View (such as a button click) to a ViewModel.

  • usage: Used on methods in ViewModel[RelayCommand]characteristic.
  • Sample code
public partial class MyViewModel : ObservableObject
{
    [RelayCommand]
    private void ExecuteCommand()
    {
        // Command execution logic here
    }
}

This will generate aICommandImplementation, can be bound by View.

Other properties

RelayCommandandAsyncRelayCommand

These classes implementICommandInterface, used to create commands, is an implementation of command mode in MVVM mode.

IMessenger

Message delivery interface, used to pass messages between different ViewModels or Views, implementing loosely coupled communication between components.

PropertyChangedMessage<T>

The message used for property changes isIMessengerA message type used in interface implementation to pass attribute change notifications.

RequestMessage<T>andAsyncRequestMessage<T>

Messages used to request a response of a specific type, whereAsyncRequestMessage<T>is an asynchronous version of the request message.

CollectionRequestMessage<T>andAsyncCollectionRequestMessage<T>

A request message for a collection operation, whereAsyncCollectionRequestMessage<T>is an asynchronous version of the collection request message.

ValueChangedMessage<T>

The message used for value changes isIMessengerA message type used in interface implementation to pass value change notifications.

Framework class

  • ObservableObject: It has been realizedINotifyPropertyChangedandINotifyPropertyChangingInterface to create a ViewModel that responds to property changes.
  • ObservableRecipient: Base class for messaging.
  • ObservableValidator: Base class for data verification.

  • Ioc: Provides containers and related services for dependency injection.

  • RelayCommand: A simple command implementation for executing a parameterless method.
  • RelayCommand<T>: A generic versionRelayCommand, used to execute the parameter-with-parameter method.
  • AsyncRelayCommand: An asynchronous command implementation used to execute asynchronous parameterless methods.
  • AsyncRelayCommand<T>: An asynchronous command implementation used to execute asynchronous method with parameters.
  • IRelayCommandRelayCommandinterface definition.
  • IRelayCommand<T>RelayCommand<T>interface definition.
  • IAsyncRelayCommandAsyncRelayCommandinterface definition.
  • IAsyncRelayCommand<T>AsyncRelayCommand<T>interface definition.

  • IMessenger: Message delivery interface, which defines the methods of message sending and subscribing.
  • WeakReferenceMessenger: A message passing class that uses weak references to avoid memory leaks.
  • StrongReferenceMessenger: A messaging class that uses strong references.
  • IRecipient<TMessage>: Message receiver interface, used to receive specific types of messages.
  • MessageHandler<TRecipient, TMessage>: Message handler, used to process specific types of messages.

  • PropertyChangedMessage<T>: Message used for property changes.
  • RequestMessage<T>: Message used to request a specific type of response.
  • AsyncRequestMessage<T>: Asynchronous versionRequestMessage<T>
  • CollectionRequestMessage<T>: Request message for collection operations.
  • AsyncCollectionRequestMessage<T>: Asynchronous versionCollectionRequestMessage<T>
  • ValueChangedMessage<T>: Message for value changes.

is a modern, fast and modular MVVM library that provides as much flexibility as possible, allowing developers to freely choose the components they want to use to combine these generated blocks in a way that best suits their needs.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.