Search code examples
visual-studiomvvmwindows-community-toolkit

Can't use ObservableProperty from the CommunityToolkit.MVVM


I'm trying to use the new ObservableProperty attribute from the CommunityToolkit.MVVM. Any time I add it, I get 17 errors such as "The type MainViewModel already contains a definition for FileToPlay", or "Type MainViewModel already defines a member called 'OnFileToPlayChanging' with the same parameter types". These are all in the MainViewModel.g.cs file.

I'm using VS 2022 Community, and the project has a WPF Application project template targeting .NET6.

Sample code that generates the error is:

namespace CorePlayer.ViewModel
{
    public partial class MainViewModel : ObservableObject
    {
        [ObservableProperty]
        private string? fileToPlay;
    }
}

Anyone have any idea what I could be doing wrong? Thanks


Solution

  • After updating Visual Studio to version 17.4.0 preview 6 (and making sure the class was marked "partial"), it now works.