WinUI3/Desktop/C++: 无法编译 winrt::resume_foreground(Microsoft::System::DispatcherQueue const& dispatcher)

问题描述

我想使用:

co_await winrt::resume_foreground(window.dispatcherQueue());

(“窗口”的类型是:“winrt::Microsoft::UI::Xaml::Window”)

但我无法编译它,因为

winrt::resume_foreground(Microsoft::System::dispatcherQueue const& dispatcher)

未定义。

我不能包含包含 dispatcherQueue 类的 #include <winrt/Microsoft.System.h>

我的环境:
Windows 10 专业版,21H1,19043.1083
Visual Studio 社区 2019 (16.10.3)
Visual Studio 扩展:Project Reunion 版本 0.8.0.46122163
项目模板:C++、空白应用、打包(桌面版 WinUI 3)

为了重现错误,我使用了上面的项目模板并将以下方法添加到“App”类中。

App.xaml.h

winrt::Windows::Foundation::IAsyncAction foo();

App.xaml.cpp

winrt::Windows::Foundation::IAsyncAction App::foo()
{
    co_await winrt::resume_foreground(window.dispatcherQueue());
}

我收到错误消息:

D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,21): error C2039: 'resume_foreground': is not a member of 'winrt'
1>D:\Solution\WinUi3 Test\WinUi3 Test\MainWindow.xaml.h(23): message : see declaration of 'winrt'
1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,38): error C3861: 'resume_foreground': identifier not found
1>Done building project "WinUi3 Test.vcxproj" -- Failed.

如果我尝试包含 #include <winrt/Microsoft.System.h>,我会得到:

1>D:\Solution\WinUi3 Test\WinUi3 Test\pch.h(25,10): Fatal error C1083: Cannot open include file: 'winrt/Microsoft.System.h': No such file or directory
1>Done building project "WinUi3 Test.vcxproj" -- Failed.

如果我包含 #include <winrt/Windows.System.h>#include <winrt/Windows.UI.Core.h>,我会得到:

1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,63): error C2665: 'winrt::resume_foreground': none of the 2 overloads Could convert all the argument types
1>D:\Solution\WinUi3 Test\WinUi3 Test\Generated Files\winrt\Windows.UI.Core.h(2805,31): message : Could be 'winrt::resume_foreground::awaitable winrt::resume_foreground(const winrt::Windows::UI::Core::Coredispatcher &,const winrt::Windows::UI::Core::CoredispatcherPriority) noexcept' (compiling source file App.xaml.cpp)
1>D:\Solution\WinUi3 Test\WinUi3 Test\Generated Files\winrt\Windows.System.h(4529,31): message : or       'winrt::resume_foreground::awaitable winrt::resume_foreground(const winrt::Windows::System::dispatcherQueue &,const winrt::Windows::System::dispatcherQueuePriority) noexcept' (compiling source file App.xaml.cpp)
1>D:\Solution\WinUi3 Test\WinUi3 Test\App.xaml.cpp(50,63): message : while trying to match the argument list '(winrt::Microsoft::UI::dispatching::dispatcherQueue)'
1>Done building project "WinUi3 Test.vcxproj" -- Failed.

解决方法

包括

#include <winrt/Microsoft.UI.Dispatching.h>
#include <Microsoft.UI.Dispatching.co_await.h>

当从 0.8.0 预览版更新到 0.8.0 时,命名空间从 Microsoft.SystemMicrosoft.UI.Dispatchingresume_foreground 现在在 Microsoft.UI.Dispatching.co_await.h 中定义。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...