| MAUI-ContentPage-ContentView :👈 | 👉:MAUI-CommunityToolkit-2 |
π Hour 1 β MVVM Basics & CommunityToolkit.Mvvm Introduction |
π Hour 1 β MVVM Basics & CommunityToolkit.Mvvm Introduction
π1. Understanding MVVM (ModelβViewβViewModel)
MVVM is a UI architectural pattern used in .NET MAUI, WPF, WinUI, Xamarin, UWP. It separates the UI from the logic.
- π Model: Represents data, entities, and backend logic.
- πΌοΈ View: The UI (XAML). Displays data from the ViewModel.
- π§ ViewModel: The brain. Exposes properties & commands for the UI.
Why MVVM?
- π Keeps UI and logic separate
- π§ Easy to test
- π¦ Reusable ViewModels
- β‘ Clean and maintainable structure
Why it matters: testability, clean separation, easier maintenance.
public partial class SampleViewModel : ObservableObject { [ObservableProperty] public partial string InputText { get; set; } = string.Empty;
public IRelayCommand SubmitCommand { get; }
public SampleViewModel()
{
Β Β SubmitCommand = new RelayCommand(Submit, CanSubmit);
}
private void Submit() { /* trigger use case */ }
private bool CanSubmit() => !string.IsNullOrWhiteSpace(InputText);
}
π§° 2. What is CommunityToolkit.Mvvm?
The .NET Community Toolkit MVVM library is a lightweight, modern MVVM framework developed by Microsoft.
It provides:
- β¨ Source generators (automatic code generation)
- π’ ObservableObject (INotifyPropertyChanged implementation)
- βοΈ ObservableProperty attributes
- ποΈ RelayCommand & AsyncRelayCommand
- π‘ Messenger for ViewModel communication
Why use this toolkit?
- π Removes boilerplate code
- π Cleaner ViewModels
- β‘ Much faster than manual implementations
π§©3. Understanding INotifyPropertyChanged
INotifyPropertyChanged is an interface that notifies the UI whenever a property changes.
Manual Implementation Example:
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get => name;
set
{
if (name != value)
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
π« This is long and repetitive β the Toolkit solves this!
π¦ 4. ObservableObject β Toolkit Implementation
ObservableObject already contains INotifyPropertyChanged logic.
Example:
public class PersonViewModel : ObservableObject
{
private string name;
public string Name
{
get => name;
set => SetProperty(ref name, value);
}
}
π No need to write events manually.
β¨ 5. Toolkit Auto-Generated Properties
You can also use the [ObservableProperty] attribute to generate properties automatically.
Example:
public partial class PersonViewModel : ObservableObject
{
[ObservableProperty]
private string name;
}
This creates:
- π A property Name
- π Automatic PropertyChanged notifications
Generated property is equivalent to this:
// Auto-generated by the toolkit
public string Name
{
get => name;
set => SetProperty(ref name, value);
}
π₯ 6. Installing MVVM Toolkit
Use this command in your project folder:
dotnet add package CommunityToolkit.Mvvm
π οΈ 7. Hands-On Tasks for hour 1
- π Create a new MAUI/WPF project
- β Add the CommunityToolkit.Mvvm NuGet package
- π§ͺ Create a new ViewModel deriving from ObservableObject
-
βοΈ Add a property using both:
- Manual INotifyPropertyChanged
- Toolkitβs [ObservableProperty]
- π Bind the property to a Label/TextBlock
- π Change the property and observe UI updates
π Hour 1 Example Practice ViewModel
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private string title = "Welcome to MVVM Toolkit";
[ObservableProperty]
private int counter;
public void Increment()
{
Counter++;
}
}
Bind this in XAML:
<Label Text="{Binding Title}" />
<Label Text="{Binding Counter}" />
Result:
- π·οΈ UI updates automatically
- β‘ No need for manual events
| MAUI-ContentPage-ContentView :👈 | 👉:MAUI-CommunityToolkit-2 |