Search code examples
c#wpfcommunity-toolkit-mvvm

Why is Visual Studio not generating the Community.Toolkit.MVVM additional code automatically?


The source generator is creating code, only if I build the solution.

Questions

  • Did I mess up Visual Studio settings?
  • How can I fix this behavior?
  • Do I miss something implementing the toolkit?

Settings

  • Visual Studio 2022
  • .Net 8
  • WPF Template
  • CommunityToolkit.MVVM 8.3.2

Problem

Please look at the first image.
The annotated field _title does have the corresponding property Title. This property was generated only after building the project.
Without building the project it looked like the annotated field _copyright. There is no code generated. The property does not exist. If the project is build, then the property does show up (or if the generator is manually triggered to re-run).

This happens with all my existing WPF-apps, although they have worked properly in spring this year (Version 8.2.2). I did not touch them since then. I have done my research, but some how I am standing on my toes.

Code

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleCommunityToolkit.ViewModels;

public partial class HomeViewModel : ObservableObject
{
    [ObservableProperty]
    private string? _title;

    [ObservableProperty]
    private string? _copyright;
    public HomeViewModel()
    {
        Title = "Hello World App";
        
    }
}

enter image description here

enter image description here


Solution

  • After going through all the settings of TextEditor/C#/CodeGeneration in VS, the answer was found.

    Selecting Automatic turned on the behavior as I knew it.

    Maybe this saves someone hours of research and frustration. I was doubting myself so much.

    Scrrenshot of VS Tools/Options/TextEditor/C#/CodeGeneration