2

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

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
ClarkG
  • 81
  • 9

2 Answers2

1

I had this exact same problem. Searching for an answer led me to this question. My application is a WPF app on .NET Core 3.1.

I fixed it by following the guidance at the end of this YouTube video, .NET Community Toolkit.

I chose my second project as a .NET Standard 2.1 project.

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <Nullable>enable</Nullable>
    <LangVersion>10.0</LangVersion>
  </PropertyGroup>
kduenke
  • 181
  • 4
0

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

ClarkG
  • 81
  • 9