我有一个从“空白应用程序”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
or中的任何位置放置一个断点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
此时,新视图可见并且可以正常工作。
为什么我必须闯入代码才能使我的新视图可见?