问题描述
我用下面的code我:
I'm using the following code for my:
protected override void OnSourceInitialized(EventArgs e)
{
...
....
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
...
...
}
在一些系统中源的价值就出来为空,我不能找到原因...
In some systems the "source" value comes out to be null and i cant find the reason why...
推荐答案
我想你可能要等到UI渲染,直到你尝试分配的HWND。尝试将它的事件处理程序Window.Loaded代替。
I think you may have to wait until the UI is rendered until you try to assign the Hwnd. Try putting it in the event handler for Window.Loaded instead.
这发生在我之前,我有HWND分配的InitializeComponent()之后被称为在code-背后的构造。它总是回来为空,这可能会感觉到,当我走进过,看到的UI也尚未出现。我打开它变成了_Loaded处理程序,瞧,用户界面击中该行之前,使所有的突然这个不再是零。
This happened to me before, I had the Hwnd assignment after InitializeComponent() was called in the code-behind's constructor. It always came back null, which may sense when I stepped through and saw the UI hadn't appeared yet. I popped it into the _Loaded handler and voila, the UI renders before hitting that line and all of the sudden 'this' stopped being null.
这篇关于presentationSource.FromVisual(本)返回在WPF空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!