【问题标题】:How to get the Window hosting a UIElement instance in C++/WinRT, WinUI 3如何在 C++/WinRT、WinUI 3 中获取托管 UIElement 实例的窗口
【发布时间】:2022-10-04 23:54:18
【问题描述】:

我正在尝试获取在 WinUI 3 中托管 UIElement 实例的 Window 实例。

有一个 helper method in the Window class in .NET (see this thread) 但我找不到 C++/WinRT 的类似内容。

我按照一些建议尝试了VisualTreeHelper,但在这里没有帮助;没有一个父母属于winrt::Microsoft::UI::Xaml::Window

是否可以获得依赖对象的托管Window

【问题讨论】:

    标签: window c++-winrt visual-tree dependencyobject


    【解决方案1】:

    如果是 UWP 应用程序,每个 UI 线程已经有一个可以使用静态 Window.Current property 检索的 Window。

    如果是 C++/WinRT WinUI3 in Desktop 应用程序,Window 实现IWindowNative 通过窗口的 HWND (WindowHandle).你可以得到处理的窗口,做你想做的事。喜欢:

     // Get the current window's HWND by passing in the Window object
         var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
    

    更多信息请查看:Window Class-Windows App SDK

    【讨论】:

    • 该问题被标记为c++-winrt。这里的代码不是 C++。
    • 我猜Window.Current() 可能是另一种选择,但 MS 写道桌面应用程序始终返回 null.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多