WinUI3+.NET6环境MsTest引用项目报资源重复错误求解决方案
WinUI 3 + MSTest 单元测试引用冲突问题
问题背景
使用C#、.NET 6.0和VS 2022开发WinUI 3应用,尝试通过MSTest添加单元测试,但在引用待测试项目时出现资源冲突错误。曾参考早期WinUI 3单元测试指导,其中部分步骤(如合并csproj与wapproj)已失效。基础单元测试可在VS测试资源管理器中正常显示,但添加待测试项目引用后,构建或运行测试时触发资源处理错误。
错误现象
首次报错信息:
Severity Code Description Project File Line Suppression State Error PRI277: 0x80073b0f - Conflicting values for resource 'Files/MainWindow.xbf' UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1 Error PRI175: 0x80073b0f - Processing Resources failed with error: Duplicate Entry. UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
移除测试项目默认资产PNG文件后,报错对象变更为App.xbf:
Severity Code Description Project File Line Suppression State Error PRI175: 0x80073b0f - Processing Resources failed with error: Duplicate Entry. UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1 Error PRI277: 0x80073b0f - Conflicting values for resource 'Files/App.xbf' UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
当前测试项目配置
测试项目的.csproj内容如下(已确保WindowsAppSDK和BuildTools版本与待测试项目一致,但原指导中部分测试包已废弃):
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework> <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion> <RootNamespace>UnitTestProject</RootNamespace> <ApplicationManifest>app.manifest</ApplicationManifest> <Platforms>x86;x64;ARM64</Platforms> <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers> <PublishProfile>win10-$(Platform).pubxml</PublishProfile> <UseWinUI>true</UseWinUI> <EnableMsixTooling>true</EnableMsixTooling> <WindowsPackageType>None</WindowsPackageType> </PropertyGroup> <ItemGroup> <ProjectCapability Include="TestContainer" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.8.0"> <ExcludeAssets>build</ExcludeAssets> </PackageReference> <PackageReference Include="Microsoft.Windows.Compatibility" Version="8.0.0" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231115000" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" /> <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" /> <PackageReference Include="MSTest.TestFramework" Version="3.1.1" /> <Manifest Include="$(ApplicationManifest)" /> </ItemGroup> <!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging Tools extension to be activated for this project even if the Windows App SDK Nuget package has not yet been restored. --> <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> <ProjectCapability Include="Msix" /> </ItemGroup> <ItemGroup> <Folder Include="Assets\" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\MyApp\MyApp.csproj" /> </ItemGroup> <!-- Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution Explorer "Package and Publish" context menu entry to be enabled for this project even if the Windows App SDK Nuget package has not yet been restored. --> <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu> </PropertyGroup> </Project>
解决建议
- 修改测试项目输出类型:将测试项目的
<OutputType>从WinExe改为Console,单元测试无需UI应用入口,避免资源打包逻辑冲突。 - 禁用PRI文件生成:在测试项目的
<PropertyGroup>中添加<GeneratePriFile>false</GeneratePriFile>,跳过资源索引文件的生成,避免与待测试项目的资源重复处理。 - 调整项目引用规则:在测试项目的
<ProjectReference>中排除待测试项目的资源文件,例如:<ProjectReference Include="..\MyApp\MyApp.csproj"> <ExcludeAssets>All</ExcludeAssets> <IncludeAssets>Compile;Build</IncludeAssets> </ProjectReference> - 拆分业务逻辑:将WinUI项目中的核心业务逻辑抽离到独立的.NET类库项目中,直接对类库进行单元测试,无需引用WinUI项目,从根源避免资源冲突问题。
- 清理并重建解决方案:执行
Clean Solution后再Rebuild Solution,清除旧的资源缓存文件。
内容的提问来源于stack exchange,提问作者Woz9
相关产品推荐
相关产品推荐

