У меня есть простое приложение 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());
}
Я ожидаю, что при нажатии кнопки 1 и вводе ClickHandler
будет создан новый вид, готовый к активации, чтобы при нажатии кнопки 2 ивведите ClickHandler2
, что мой вновь созданный вид активируется и, таким образом, становится видимым.
Вместо этого происходит то, что вид не изменяется, и я получаю следующий вывод в консоли:
Я нажимаю кнопку1
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
В этот момент новый виден, и он работает.
Почему я должен взломать код, чтобы мой новый вид стал видимым?