[英]How can we add a NavigationViewItem in C++/WinRT in a WinUI3 desktop app?
[英]WinUI3/Desktop/C++: Can't compile 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”类。
应用程序.xaml.h
winrt::Windows::Foundation::IAsyncAction foo();
应用程序.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.System
更改为Microsoft.UI.Dispatching
并且resume_foreground
现在在Microsoft.UI.Dispatching.co_await.h
定义。
Markus 的回答在 WinUI3 1.0 版本中对我不起作用,因为Microsoft.UI.Dispatching.co_await.h
头文件不存在。 但是,以下确实有效:
#include <wil/cppwinrt.h>
#include <wil/cppwinrt_helpers.h>
co_await wil::resume_foreground(DispatcherQueue());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.