Why do my source generated properties are unreacheable in code? using mvvm toolkit

314 Views Asked by At

Hello got the following wpf code thats using source generators utlizing mvvm community toolkit version 8:

      public partial class CalculatorVM : ObservableObject
       {
        [ObservableProperty]
        private  int buttonOne = 1;

        [ObservableProperty]
        private  int buttonTwo = 2;

        [ObservableProperty]
        private  int buttonThree = 3;

        [ObservableProperty]
        private  int buttonFour = 4;

        [ObservableProperty]
        private  int buttonFive = 5;

        [ObservableProperty]
        private  int buttonSix = 6;

        [ObservableProperty]
        private  int buttonSeven = 7;

        [ObservableProperty]
        private  int buttonEight = 8;

        [ObservableProperty]
        private  int buttonNine = 9;

        [ObservableProperty]
        private double result, lastNumber;

        [ObservableProperty]
        SelectedOperator selectedOperator;

        [ObservableProperty]
        private int clickedNumber;

        [ObservableProperty]
        private Label resultLabel;



        [RelayCommand]
        public void NumbercClicked(int number)
        {
            var selectedNumber = number switch
            {
                1 => ButtonOne,
                2 => ButtonTwo,
                3 => ButtonThree,
                4 => ButtonFour,
                5 => ButtonFive,
                6 => ButtonSix,
                7 => ButtonSeven,
                8 => ButtonEight,
                9 => ButtonNine,

            };
            ClickedNumber = selectedNumber;
            if (ClickedNumber == 0) Result = 0;
            else Result = Result + selectedNumber;


        }

I cant compile, tells me buttonOne etc dont exsist in the current context, The Repo: https://github.com/KostaKing/Calculator/tree/master/Calculator/ViewModels Anybody has any idea?

1

There are 1 best solutions below

0
Andy On

You have old format csproj.

These explicitly list the files which are part of a project:

  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    <Compile Include="ViewModels\CalculatorVM.cs" />
    <Page Include="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="MainWindow.xaml.cs">
      <DependentUpon>MainWindow.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs">
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>

When the code generator creates a new file in a new format project, that is automatically going to be included in the project. New style projects include everything in their folders.

Old style ones only include what's listed in ItemGroup.

If your partial classes are being created then this is why they will be ignored.

You will need to upgrade the csproj to a new format.

The simplest way to do that is often to create a new solution and projects, copy in source files out your old solution.

You can target .net 4.8 if you really want to with a new format csproj. To do so be careful about which wpf project template you choose. There are two in vs2022.