Home > Net >  Can't use ObservableProperty from the CommunityToolkit.MVVM
Can't use ObservableProperty from the CommunityToolkit.MVVM

Time:08-09

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 (in braces - S.O. won't let me use them here)
        private string? fileToPlay;
    }
}

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

CodePudding user response:

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>
  • Related