gpt4 book ai didi

c# - PresentationSource.FromVisual(this) 在 WPF 中返回空值

转载 作者:太空狗 更新时间:2023-10-29 18:11:03 25 4
gpt4 key购买 nike

我正在使用以下代码:

protected override void OnSourceInitialized(EventArgs e)
{
...
....
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
...
...
}

在某些系统中,“源”值结果为空,我找不到原因......

最佳答案

我认为您可能必须等到 UI 呈现后再尝试分配 Hwnd。尝试将其放入 Window.Loaded 的事件处理程序中。

这发生在我身上,在代码隐藏的构造函数中调用 InitializeComponent() 后,我有 Hwnd 赋值。它总是返回 null,当我逐步查看并看到 UI 尚未出现时可能会感觉到。我将它弹出到 _Loaded 处理程序中,瞧,UI 在到达该行之前呈现,突然间“this”不再为 null。

关于c# - PresentationSource.FromVisual(this) 在 WPF 中返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com