我将 MSTest 添加到 WinUI 3 项目中。我使用上下文菜单项“创建单元测试”创建了一个单元测试文件。然后我在构建 MSTest 项目时遇到以下错误。 怎么解决?
Error Project '..\xxx\xxx\xxx.csproj' targets 'net6.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v7.0'.
error NU1201: Project xxx is not compatible with net7.0 (.NETCoreApp,Version=v7.0). Project xxx supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
WinUi 3 项目文件 xxx.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>HP.EasyShell.Primus</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
</PropertyGroup>
MSTest 项目 testproject.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xxx\xxx\xxx.csproj" />
</ItemGroup>
</Project>
您的 WinUI 项目似乎以 .NET 7 为目标,但您的 MS Test 项目以 .NET 6 为目标。解决方案可能很简单,只需将测试项目的目标框架更改为 .NET 7 即可。
我在更改框架版本方面遇到了一些挑战,通过删除每个受影响项目的
bin
和 obj
文件夹中的所有文件可以轻松解决这些问题。请考虑删除解决方案根目录下的 .vs
文件夹,然后重新打开 Visual Studio。
如果使用 Git 进行版本控制,您始终可以选择核选项,并从命令行运行
git clean -fdx
。请注意,这会删除所有未跟踪的文件和文件夹,包括您告诉 Git 忽略的跟踪文件的更改。