MVVM Source Generators: Never Write MVVM Boilerplate Code Again!

James Montemagno · 81K views · 2022 · 19:25

Open on YouTube

Software architecture & designTesting, debugging & developer toolstutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when explaining or introducing CommunityToolkit.Mvvm and how its source generators cut MVVM boilerplate in .NET apps.

Recommended by 1 people across 1 different months, 2022 to 2023.

What it is

James Montemagno walks through the .NET Community Toolkit's MVVM source generators, showing how attributes like ObservableProperty and relay commands replace hand-written INotifyPropertyChanged and ICommand boilerplate. It is a 19-minute tutorial with demo code that starts with an MVVM primer and then covers installing the toolkit and using each generator.

What people said

Recommends Andrew KeepCoding · 2022-09 · Stack Overflow · on “Does WinUI support MVVM?”

Actually, the **CommunityToolkit.Mvvm** package is reducing a bunch of boilerplate code using source-generators. IMHO, this is an awesome package. You can see yourself in this [this video] tutorial form James Montemagno.

Recommends Andrew KeepCoding · 2022-09 · Stack Overflow · on “Creating dynamic data entry form from the list of fields chosen by User at run time”

This [this video] is a good tutorial for this. Just one thing. In this tutorial uses ``[ICommand]`` but this was renamed to ``[RelayCommand]``.

Cites Ray · 2022-03 · Stack Overflow · on “HttpClient.GetAsync not re-enabling the button MVVM”

Solved: See below. Update: This fails on the HttpClient.GetAsync. It's like a "Task Finished" event isn't listened to. I'm suspecting a threading problem. It manifests itself as the button not re-enabling. I'm getting to know the CommunityToolkit.Mvvm as introduced in James Montemagno's YouTube Channel: [this video] I have added an command button that calls a function that makes a call to a web api, that takes a couple of seconds to complete, but does complete successfully. Everything is working fine but for one thing. The button disables on click, but never re-enables after the Task has finished. The RefreshButtonText is set to "Starting" and then "Finished" as expected. What am I missing? :) [I

Cites Tarazed · 2023-12 · Stack Overflow · on “Avalonia MVVM Toolkit Source Generator call method or raise event not fully working”

You don't need to invoke the PropertyChanged event yourself, this is already done as part of the generated code. (You can see this generated code if using visual studio, open the Anaylzers from the solution explorer, open CommunityToolkit.Mvvm.SourceGenerators, and then open the CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator and find your class). By implementing CommunityToolkit you are saving yourself the trouble of doing all this boiler plate code. If you need to respond to your own PropertyChanged event, it is protected virtual and you can override it. protected override void OnPropertyChanged(PropertyChangedEventArgs e) { // Do something here // then call base. base.O