簡體   English   中英

向 .NET 6 項目添加新類會導致構建錯誤 NETSDK1022 (VS2022)

[英]Adding new class to .NET 6 project results in build error NETSDK1022 (VS2022)

我最近在我的解決方案中添加了一個新項目。 解決方案中的所有項目都是用 C# 編寫的 .NET 6 項目。 (使用 Visual Studio 2022 版本 17.2.0)

每次我向這個新項目添加一個新類。 Visual Studio 莫名其妙地將其添加到 .csproj 文件中(作為項目組內的<Compile/>標記)。 這會導致構建錯誤。

為了說明,剛才我在項目中添加了一個測試類(在文件“Class.cs”中)然后構建。 這是錯誤:

4>C:\ProgramFiles\dotnet\sdk\6.0.300\Sdks\Microsoft.NET.Sdk\targets\
Microsoft.NET.Sdk.DefaultItems.Shared.targets(190,5): error NETSDK1022:
Duplicate 'Compile' items were included. The .NET SDK includes 'Compile'
items from your project directory by default. You can either remove these
items from your project file, or set the 'EnableDefaultCompileItems' 
property to 'false' if you want to explicitly include them in your project
file. For more information, see https://aka.ms/sdkimplicititems. The 
duplicate items were: 'ViewModels\Class1.cs'

果然,這是添加到 .csproj 文件中的行

  <Compile Include="ViewModels\Class1.cs" />

如果我手動然后從項目文件中刪除該行,它構建得很好。

奇怪的是它只適用於這個項目。 如果我對解決方案中的任何其他項目執行相同的操作,VS不會將文件添加到項目中,並且不會出現構建錯誤。

有什么設置可以用來解決這個問題嗎?

僅供參考:這是該項目的初始屬性組。 我有另一個具有相同標題的應用程序項目沒有出現此問題

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <PlatformTarget>x64</PlatformTarget>
    <AssemblyName>GelSight.VerifyDevice</AssemblyName>  
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <BaseOutputPath>..\..</BaseOutputPath>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Platforms>x64</Platforms>
    <Nullable>enable</Nullable>
</PropertyGroup>

EnableDefaultCompileItems屬性設置為false

如果您在項目文件中明確定義這些項目中的任何一項,您可能會收到類似於以下內容的“NETSDK1022”構建錯誤:

包括重復的“編譯”項目。 默認情況下,.NET SDK 包含項目目錄中的“編譯”項。 您可以從項目文件中刪除這些項目,或者如果您想將它們顯式包含在項目文件中,則將“EnableDefaultCompileItems”屬性設置為“false”。

包含重復的“EmbeddedResource”項目。 默認情況下,.NET SDK 包含項目目錄中的“EmbeddedResource”項。 您可以從項目文件中刪除這些項目,或者如果要將它們明確包含在項目文件中,則將“EnableDefaultEmbeddedResourceItems”屬性設置為“false”。

要解決錯誤,請執行以下操作之一:

  • 刪除與上表中列出的隱式項匹配的顯式 Compile、EmbeddedResource 或 None 項。

  • 將 EnableDefaultItems 屬性設置為 false 以禁用所有隱式文件包含:

<PropertyGroup>
  <EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
  • 通過將 EnableDefaultCompileItems、EnableDefaultEmbeddedResourceItems 或 EnableDefaultNoneItems 屬性設置為 false,選擇性地僅禁用 Compile、EmbeddedResource 或 None glob:
<PropertyGroup>
  <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
  <EnableDefaultNoneItems>false</EnableDefaultNoneItems>
</PropertyGroup>

如果您只禁用編譯 glob,Visual Studio 中的解決方案資源管理器仍將 *.cs 項顯示為項目的一部分,包括為 None 項。 要禁用隱式 None glob,請將 EnableDefaultNoneItems 也設置為 false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM