-2

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?

AntiMatter
  • 13
  • 5
  • have you tried to use Properties instead of Field? eg `private int buttonOne {get; set;} = 1;` – gepa Mar 07 '23 at 12:04
  • I could do that but that would beat the purpose, trying to learn the mvvm toolkit – AntiMatter Mar 07 '23 at 12:05
  • It seems MVVM Source Generators don't support older .net frameworks, you need to upgrade your project to NET 6 – gepa Mar 07 '23 at 12:16
  • This might help [Upgrade a WPF App to .NET 6 with the .NET Upgrade Assistant](https://learn.microsoft.com/en-us/dotnet/core/porting/upgrade-assistant-wpf-framework) – gepa Mar 07 '23 at 12:17
  • IT Does, check this out: https://stackoverflow.com/questions/75443376/community-toolkit-mvvm-doesnt-bind-data-between-view-and-view-model-using-wpf In addition the code is being generated, i just cant access it – AntiMatter Mar 07 '23 at 12:21

1 Answers1

0

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.

Andy
  • 11,864
  • 2
  • 17
  • 20