我正在按照此文档使用 MSTest 对 WinUi 3 项目进行单元测试。
https://devblogs.microsoft.com/ifdef-windows/winui-desktop-unit-tests/
从 App.xaml.cs 文件中编辑 OnLaunched 方法后出现此构建错误:
error CS7069: Reference to type 'DispatcherQueue' claims it is defined in 'Microsoft.WinUI', but it could not be found.
App.xaml.cs:
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
...
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();
m_window = new MainWindow();
// Ensure the current window is active
m_window.Activate();
UITestMethodAttribute.DispatcherQueue = m_window.DispatcherQueue; //Error
// Replace back with e.Arguments when https://github.com/microsoft/microsoft-ui-xaml/issues/3368 is fixed
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine);
}
这是关于相同错误的问题。 https://github.com/microsoft/testfx/issues/993
这个解决方案意味着什么?我仍然不知道如何解决这个错误。
DispatcherQueue is moved to Microsoft.UI.Dispatching namespace. You might want to check your references.
环境:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>WelcomeScreen</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>
</PropertyGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
<ItemGroup>
<ProjectCapability Include="TestContainer" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230913002" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter">
<Version>3.2.2</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>2.2.4</Version>
</PackageReference>
<PackageReference Include="Microsoft.TestPlatform.TestHost">
<Version>17.10.0-preview-24080-01</Version>
<ExcludeAssets>build</ExcludeAssets>
</PackageReference>
</ItemGroup>
自该博客发布以来,似乎发生了一些重大变化。将 NuGet 包更新到最新版本应该可以修复该编译错误。
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter">
<Version>3.2.2</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>3.2.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.TestPlatform.TestHost">
<Version>17.9.0</Version>
<ExcludeAssets>build</ExcludeAssets>
</PackageReference>
</ItemGroup>