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
Model ViewModel View

🧰 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
Back to Index
MAUI-ContentPage-ContentView :👈 👉:MAUI-CommunityToolkit-2
*