Open AndreyMarten opened 1 year ago
So you are trying to get mouse position on a window that you closed and are getting a first chance exception, basically hitting this?
Looks working as designed to me.
Hello,
I'm afraid that's not quite so.
We call the Mouse.GetPosition
method for the Border element declared inside TabItem. This TabItem belongs to MainWindow which remains visible during the application's life. The method, in turn, is called for the MainWindow object. Please refer to the corresponding code in MainWindow.xaml.cs:
private void Border_Loaded(object sender, RoutedEventArgs e)
{
var pt = Mouse.GetPosition(this);
}
Here, this
is MainWindow, not a child Window.
We do not call GetPosition
for the child window at all. And we close only this child window.
So, even though we call the GetPosition for MainWindow which is still available, the Invalid window handle.
exception is thrown in this case.
Thanks, Andrey
Ah I see. It's because that child window is the active source at the time, which is why not moving the mouse is critical.
One option to fix this would be for the
to also check inputSource.IsDisposed
.
Problem description:
An attempt to call the
Mouse.GetPosition
method for an UI element in itsLoaded
event handler right after a child window was closed lead to the "System.ComponentModel.Win32Exception: 'Invalid window handle.'" error. This issue occurs only if the mouse pointer is over the UI element that is loaded.NOTE: You need to enable
Common Language Runtime Exceptions
to see this error.Please refer to the Minimal repro and Screencast sections that illustrate the problematic behavior.
Actual behavior:
Expected behavior:
The
Mouse.GetPosition
method should return a correct Point value.Minimal repro: dxSample.zip Screencast: screencast.mp4
Steps to reproduce:
Common Language Runtime Exceptions
in your Visual Studio;Test
button in the MainWindow and do not move the mouse pointer anywhere;At this moment, an error should occur in the Loaded event handler of the Border element. Please refer to the screencast to see the issue.