

Code snippets for CommunityToolkit.Mvvm
source link: https://www.andreasnesheim.no/code-snippets-for-communitytoolkit-mvvm/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK