我有一个从“空白应用程序”视觉工作室模板创建的简单cppWinRT应用程序。我用下面的处理程序添加了2个按钮:
Windows::Foundation::IAsyncAction MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
OutputDebugStringW((L"\n Entered the function : " + std::to_wstring(GetCurrentThreadId()) + L"\n").c_str());
coreView = winrt::Windows::ApplicationModel::Core::CoreApplication::CreateNewView();
OutputDebugStringW((L"\n Created the view : " + std::to_wstring(GetCurrentThreadId()) + L"\n").c_str());
co_await resume_foreground(coreView.Dispatcher());
auto appView = winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
m_window_debug = Windows::UI::Core::CoreWindow::GetForCurrentThread();
OutputDebugStringW((L"\n Switched thread : " + std::to_wstring(GetCurrentThreadId()) + L"\n").c_str());
hstring newTitle = L"my new window";
appView.Title(newTitle);
OutputDebugStringW((L"\n Set new title : " + std::to_wstring(GetCurrentThreadId()) + L"\n").c_str());
m_window_debug.Activated([&, this](auto&& sender, auto&& e) {
OutputDebugStringW((L"\n sender ActivationMode : " + std::to_wstring(static_cast<int>(sender.ActivationMode())) + L"\n").c_str());
OutputDebugStringW((L"\n sender ActivationState : " + std::to_wstring(static_cast<int>(e.WindowActivationState())) + L"\n").c_str());
});
OutputDebugStringW((L"\n Registered the callback : " + std::to_wstring(GetCurrentThreadId()) + L"\n").c_str());
}
Windows::Foundation::IAsyncAction MainPage::ClickHandler2(IInspectable const&, RoutedEventArgs const&)
{
co_await resume_foreground(coreView.Dispatcher());
Windows::UI::Core::CoreWindow::GetForCurrentThread().Activate();
OutputDebugStringW((L"\n After activation : " + std::to_wstring(static_cast<int>(m_window_debug.ActivationMode())) + L"\n").c_str());
}我希望当我单击button1并输入ClickHandler时,将创建一个新视图并准备激活,这样当我单击button2并输入ClickHandler2时,我新创建的视图将被激活并变为可见。
相反,发生的情况是视图不变,并且我在控制台中得到以下输出:
我单击Button1
Entered the function : 33084
Created the view : 33084
Switched thread : 8928
Set new title : 8928
Registered the callback : 8928我单击Button2
After activation : 0现在奇怪的是,如果我在ClickHandler或ClickHandler2中的任何地方放置一个断点,然后按F10跳过,然后按F5继续,它就可以工作了,并且新的视图可以看到新的标题。输出如下所示:
我单击Button1
Entered the function : 32432
Window created : 5268
Created the view : 32432
Switched thread : 5268
Set new title : 5268
Registered the callback : 5268我单击Button2,在ClickHandler2中断开行,跳过并继续。
After activation : 0
sender ActivationMode : 3
sender ActivationState : 0
sender ActivationMode : 1
sender ActivationState : 1在这一点上,新的视图是可见的,并且它可以工作。
为什么我必须进入代码才能让我的新视图变得可见?
发布于 2018-08-30 14:13:01
作为winrt::resume_foreground的文档,此接口支持的最低SDK: Windows SDK Insider预览版10.0.17661.0 (Windows10,版本1803)。如果您使用SDK版本17134在设备版本17134上对其进行测试,则它不是匹配环境。
您可以在OS insider预览版等于或高于17661的设备上测试您的项目,并将应用目标版本更改为对应的insider预览版SDK版本。参见Windows Insider Preview Downloads。我试着用UWP应用程序目标版本17744,最低版本17134在操作系统版本17746的设备上测试你的样本,它工作得很好。
https://stackoverflow.com/questions/52030951
复制相似问题