Search code examples
c#mvvmcommunity-toolkit-mvvm

Unable to find code generated for [ObservableProperty]


I am trying to get into App Programming and wanted to learn the basics about MAUI and MVVM.

With the following code:

public partial MainViewModel : ObservableObject
{
    [ObservableProperty]
    string? text;
}

I'm expecting to see the code generated for that [ObservableProperty] in my project under Dependencies => Analyzers => CommunityToolkit.Mvvm.SourceGenerators => CommunityToolkit.Mvvm.SourceGenerators.ObservableObjectGenerator but all I have is a message that says:

This generator is not generating files.

I tried to build a whole new project and start all over again. But it didn't work as well. I checked on correct C# version and CommunityToolkit.Mvvm version.

Does anybody know how to fix this?


Solution

  • Your code and project's setup are fine, you're simply looking in the wrong place. The source generator that deals with [ObservableProperty] is CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator.

    The one you were looking at (ObservableObjectGenerator) deals with [ObservableObject]. For example it'd have generated something if you had the following code instead:

    [ObservableObject]
    public partial class MainViewModel
    {
        [ObservableProperty]
        string? text;
    }