【问题标题】:Can't activate CoreWindow in cppWinRT UWP app无法在 cppWinRT UWP 应用中激活 CoreWindow
【发布时间】: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

现在奇怪的是,如果我在 ClickHandlerClickHandler2 中的任何位置放置断点,然后按 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

标签: uwp c++-winrt


【解决方案1】:

作为winrt::resume_foreground 的文档,此 API 的最低支持 SDK:Windows SDK Insider Preview 版本 10.0.17661.0(Windows 10,版本 1803)。如果在 17134 版本的设备上使用 SDK 版本 17134 进行测试,则不是匹配环境。

您可以在 OS Insider Preview 版本等于或高于 17661 的设备上测试您的项目,并将您的应用目标版本更改为相应的 Insider Preview SDK 版本。见Windows Insider Preview Downloads。我尝试在操作系统版本为 17746 的设备上使用 UWP 应用目标版本 17744、最低版本 17134 测试您的示例,它运行良好。

【讨论】:

  • 感谢您提供的信息。我确实得到了那个预览版本,并设法让应用程序编译和运行(我说的是我在上一条评论中链接的名为 so_repro 的应用程序)。确实,当您单击按钮时它会显示一个窗口,但是请注意该窗口未激活(因此是此问题的标题)。我尝试使用 Window::Current().CoreWindow().Activate() 手动激活它,但它仍然没有激活它。从那时起,我发现我需要向窗口添加一个 Xaml Frame 元素才能激活它,因为这是一个 xaml 应用程序。
  • 向视图添加 Xaml 框架实际上会导致我的用例出现一个非常具体的问题,因为我想使用 CoreWindow 创建 DirectX 交换链。事实证明,如果我向其中添加 Xaml 框架,对 IDXGIFactory2::CreateSwapChainForCoreWindow 的调用将失败,并出现“拒绝访问”错误。所以现在的问题是,如何在不添加 xaml 框架的情况下激活新视图。
  • 您可以尝试添加一个Frame,然后将SwapChainPanel放入Frame中。 SwapChainPanel 提供了一个托管界面,其中 Microsoft DirectX 交换链提供了可以呈现到 XAML UI 中的内容。另见XAML SwapChainPanel DirectX interop sample的代码。
  • 是的,这实际上已经在做 :) 问题是我绝对需要第二个视口,它使用 CreateSwapchainForComposition 和 CoreWindow,因为用于 Visual Studio 的 nvidia nsight 调试器仅适用于此。
  • 我有一个单独的问题,可以更详细地解释我刚才在评论中所说的话:stackoverflow.com/questions/51807703/…
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 2016-04-28
  • 2020-10-31
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多