【发布时间】:2018-08-30 14:13:01
【问题描述】:
我有一个从“空白应用”Visual Studio 模板创建的简单 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
此时,新视图可见并且可以正常工作。
为什么我必须闯入代码才能让我的新视图可见?
【问题讨论】:
-
您的应用目标版本是什么?您能否提供一个简单的示例项目来帮助我查看此问题?
-
这是一个显示相同问题的示例:github.com/maximebl/so_repro