Code snippets for CommunityToolkit.Mvvm

If you’ve ever used MVVM with .NET, you’ve definitely written the boilerplate code for observable properties many times. This can get quite tedious, which is why I was super excited when I heard about the package CommunityToolkit.Mvvm. The latest prerelease package uses source generators to simplify this process. James Montemagno covers this in depth in his video and I highly recommend that you check it out.

Inspired by this, I wanted to make some code snippets so that you can easily take advantage of this new way of creating observable properties and commands. The prerequisite for using these snippets is that you have installed version 8.0.0.-preview3 of this package into your project. For instructions on how to import code snippets into Visual Studio, follow this guide.

ObservableProperty

The first snippet is for creating an ObservableProperty using this new technique. The shortcut to use this is propop:

Here’s how it looks in action:

Code snippet in use for ObservableProperty.

Command

The second snippet is for creating an ICommand. The shortcut to use this is cmd:

Here’s how it looks:

Code snippet in use for ICommand.

Async command

The final snippet is for creating an async ICommand. The shortcut to use this is cmda:

In action:

Code snippet in use for async ICommand.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.