• Stars
    star
    212
  • Rank 186,122 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MvvmGen is a lightweight MVVM library for XAML applications. It generates your ViewModels on-the-fly for you via a Roslyn-based C# Source Generator.

âš¡ MvvmGen

Build MvvmGen NuGet MvvmGen NuGet MvvmGen

Your Friend Who Writes the Boilerplate for You

Hey there, welcome to the MvvmGen repository. MvvmGen is a lightweight and modern MVVM library (.NET Standard 2.0) built with C# Source Generators that helps you to apply the popular Model-View-ViewModel-pattern (MVVM) in your XAML applications that you build with WPF, WinUI, Uno Platform, Avalonia, Xamarin Forms, or .NET MAUI.

MvvmGen is licensed under the MIT license.

Get Started

Quick intro

In this quick intro, you'll learn that creating a ViewModel is a lot of fun with MvvmGen! 🔥

Installing the MvvmGen NuGet Package

Reference the NuGet package MvvmGen in your .NET application, and then you're ready to go:

Install-Package MvvmGen

MvvmGen will register itself as a C# source generator in your project, and it will be your friend who writes the boilerplate for you.

Generating a ViewModel class

To generate a ViewModel class, you create a new class, you mark it as partial, and you put MvvmGen's ViewModel attribute on the class:

using MvvmGen;

namespace MyWpfApp.ViewModel
{
  [ViewModel]
  public partial class EmployeeViewModel
  {
  }
}

The ViewModel attribute tells MvvmGen to generate another partial EmployeeViewModel class. Right now, it will be a class that looks like this:

using MvvmGen.Commands;
using MvvmGen.Events;
using MvvmGen.ViewModels;

namespace MyWpfApp.ViewModel
{
    partial class EmployeeViewModel : ViewModelBase
    {
        public EmployeeViewModel()
        {
            this.OnInitialize();
        }

        partial void OnInitialize();
    }
}

You can see that generated class in Visual Studio under Dependencies->Analyzers: Generated class

Beside the ViewModel attribute, you find many other attributes in the MvvmGen namespace that you can use to decorate your ViewModel class. These attributes allow you to build a full ViewModel like this:

using MvvmGen;
using MvvmGen.Events;

namespace MyWpfApp.ViewModel
{
  public record EmployeeSavedEvent(string FirstName, string LastName);

  [Inject(typeof(IEventAggregator))]
  [ViewModel]
  public partial class EmployeeViewModel
  {
    [Property] private string _firstName;
    [Property] private string _lastName;

    [Command(CanExecuteMethod = nameof(CanSave))]
    private void Save()
    {
      EventAggregator.Publish(new EmployeeSavedEvent(FirstName, LastName));
    }

    [CommandInvalidate(nameof(FirstName))]
    private bool CanSave()
    {
      return !string.IsNullOrEmpty(FirstName);
    }
  }
}

For this ViewModel, MvvmGen will generate the following partial class definition for you

using MvvmGen.Commands;
using MvvmGen.Events;
using MvvmGen.ViewModels;

namespace MyWpfApp.ViewModel
{
  partial class EmployeeViewModel : ViewModelBase
  {
    public EmployeeViewModel(MvvmGen.Events.IEventAggregator eventAggregator)
    {
      this.EventAggregator = eventAggregator;
      this.InitializeCommands();
      this.OnInitialize();
    }

    partial void OnInitialize();

    private void InitializeCommands()
    {
      SaveCommand = new DelegateCommand(_ => Save(), _ => CanSave());
    }

    public DelegateCommand SaveCommand { get; private set; }

    public string FirstName
    {
      get => _firstName;
      set
      {
        if (_firstName != value)
        {
          _firstName = value;
          OnPropertyChanged("FirstName");
        }
      }
    }

    public string LastName
    {
      get => _lastName;
      set
      {
        if (_lastName != value)
        {
          _lastName = value;
          OnPropertyChanged("LastName");
        }
      }
    }

    protected MvvmGen.Events.IEventAggregator EventAggregator { get; private set; }
    
    protected override void InvalidateCommands(string? propertyName)
    {
      base.InvalidateCommands(propertyName);
      if(propertyName == "FirstName")
      {
          SaveCommand.RaiseCanExecuteChanged();
      }
    }
  }
}

To learn all the details, go to the documentation in this repo.

More Repositories

1

mvvmgen-samples

Contains sample applications built with .NET, XAML, and MvvmGen.
C#
38
star
2

WinUI3-WebView2-Hosting-BlazorApp

C#
35
star
3

HideShowIconTray

Hides and shows the icon tray in Windows 11
C#
30
star
4

WPFandMVVM_TestDrivenDevelopment

Contains the project for the Pluralsight-course "WPF and MVVM: Test Driven Development of ViewModels"
C#
29
star
5

Uwp-Visual-Studio-Shell

C++
25
star
6

Wpf-Grid-Extensions

Contains a little extension for the WPF Grid to create RowDefinitions and ColumnDefinitions with a ShortHand Syntax
C#
13
star
7

Getting-Started-with-TypeScript

Contains the samples of the book "Getting Started with TypeScript"
JavaScript
12
star
8

Uwp-Tab-Control-Spike

Simple prototype that tries to imitate a classic TabControl like it's used in desktop applications
C#
11
star
9

Wpf-Calling-Win10-WinRT-Toast-Api

C#
9
star
10

Wpf-Hosting-Uwp-MapControl

C#
7
star
11

Blazor-Using-Custom-Web-Component

HTML
6
star
12

WinUI.Desktop.Extensions

Contains extensions for WinUI desktop applications
C#
5
star
13

Blazor-Training

HTML
4
star
14

WpfPlanetSimulation

Simple example how to let an element flow around in a circle
C#
4
star
15

Uwp-TreeView-Extensions

C#
4
star
16

Basta-TypeScript-Workshop

Enthält die Beispiele für den TypeScript Workshop an der BASTA! Konferenz
JavaScript
4
star
17

Uwp-Simple-DispatcherQueue-Samples

C#
3
star
18

EventHub.RestClientGenerator

Generates the Shared Access Signature and a Rest Client for UWP to push messages via REST into an Azure Event Hub.
C#
3
star
19

Uwp-Wpf-MVVM-NetStandard

C#
2
star
20

BetterCode2021_WinUISample

C#
2
star
21

autofac-mvvm-dependencyinjection-sample

Shows simple MVVM detail ViewModel construction with DI (Autofac)
C#
2
star
22

git-for-beginners-my-project

Repository of Git for Beginners course
2
star
23

Pluralsight-Wpf-MVVM-EF-FriendOrganizer

Contains the FriendOrganizer app of the Pluralsight course "Building an Enterprise App with WPF, MVVM, and Entity Framework Code First"
C#
2
star
24

TypeScript-Tutorial-Samples

TypeScript Beispiele für TypeScript Tutorial auf www.tutorials.entwickler.de
JavaScript
1
star
25

Visual-Studio-2019-Extension-Show-Hide-Preview-Label

A Visual Studio 2019 extension that allows you to show/hide the Preview label
C#
1
star
26

EventHub.Clients

C#
1
star
27

Uwp-x-Bind-BindBack-Sample

C#
1
star
28

EventHub.EventReader

Little application to read events from event hub. The release tab contains the binaries
C#
1
star