5

I have a .NET 5 WPF project, in the csproj file, i have

    <ItemGroup>
        <ViewModelFiles Include="**\*ViewModel.cs"></ViewModelFiles>
        <Content Include="@(ViewModelFiles)">
            <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('Model', '.xaml'))</DependentUpon>
        </Content>
    </ItemGroup>

However, whenever I add a new item into my project, the IDE automatically adds a line of remove into the csproj file. For example, after Visual\Settings\Control\InputActionViewModel.cs is added:

    <ItemGroup>
        <ViewModelFiles Include="**\*ViewModel.cs"></ViewModelFiles>
        <ViewModelFiles Remove="Visual\Settings\Control\InputActionViewModel.cs" />
        <Content Include="@(ViewModelFiles)">
            <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('Model', '.xaml'))</DependentUpon>
        </Content>
    </ItemGroup>
</Project>

This is troblesome as I need to make an additional manual operation for every item I created. I would like to know how to swich off this auto generation.

October 25, 2022 Edit

The following is my current code

  <ItemGroup>
    <Compile Update="**\*ViewModel.cs">
      <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('Model', '.xaml'))</DependentUpon>
    </Compile>
  </ItemGroup>
martinrhan
  • 362
  • 1
  • 18
  • Please provide more informaion: VS version, a minimum but complete project file which reproduces the issue, directory structure. – stijn Sep 19 '21 at 07:17
  • @martinrhan Did you manage to resolve it or work around it somehow? I have the same issue: https://github.com/dotnet/project-system/issues/8636 – Tom Pažourek Oct 25 '22 at 05:59
  • @TomPažourek Try change "Include" to "Update", I just added some detail to this post. – martinrhan Oct 25 '22 at 09:34
  • Take a look at this post https://stackoverflow.com/questions/69193872/in-msbuild-how-to-set-item-metadata-dynamically-relative-to-file-name – martinrhan Oct 25 '22 at 09:45
  • @martinrhan Thanks. For me, the final workaround that I used was `Condition="'$(DesignTimeBuild)' != 'true'"` in the ItemGroup around the wildcard include item... – Tom Pažourek Oct 26 '22 at 06:46

1 Answers1

-2

I think this is because this line

<ViewModelFiles Include="**\*ViewModel.cs"></ViewModelFiles>

has clarified that include the *ViewModel.cs files. And also, you are using new SDK project, the added items(files) will be included in the project automatically. So, twice, and MSBuild automatically remove the items which named *ViewModel.cs.

If you test to change names when adding items, such as ViewModelXXX.cs, or ViewXXXModel.cs, there will not be a line added.

If you test to add items first, and then add this line

<ViewModelFiles Include="**\*ViewModel.cs"></ViewModelFiles>

there will also not be a line added.

Tianyu
  • 895
  • 3
  • 7
  • 1
    This is probably true, but you have to understand what the issue is. @martinrhan wants to extend the msbuild project, and doesn't want visual studio to work against him. – LunicLynx Feb 02 '23 at 08:19