![](/img/trans.png)
[英]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.