2

「空のアプリ」ビジュアル スタジオ テンプレートから作成された単純な 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

奇妙なことに、 または のいずれかにブレークポイントを設定し、ClickHandlerF10ClickHandler2を押してステップ オーバーし、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

この時点で、新しいビューが表示され、機能します。

新しいビューを表示するには、どうしてコードに侵入しなければならないのですか?

4

1 に答える 1