WinRT.Interop.WindowNative.GetWindowHandle Exception: The Specified Cast is not Valid #7951
-
We are currently encountering an exception with this call: IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); The exception message is 'The specified cast is not valid'. The issue is not limited to the parent control. Any UIElement based object passed into that call will cause the exception. Our current project requires the underlying hWnd handle to integrate with 3rd party 3D graphics APIs and any information to resolve this exception would be appreciated. We have tested different versions of the Windows App SDK (microsoft.windowsappsdk) and get the same result. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
this has to be of type Window Objects of type UIElement are not child windows like regular Win32 programs. If you use Spy++ tool, you will know that DirectX is used to draw the UI. |
Beta Was this translation helpful? Give feedback.
-
Yes, this is by design. The intended way to get graphics content into Xaml is via SurfaceImageSource, VirtualSurfaceImageSource or SwapChainPanel. |
Beta Was this translation helpful? Give feedback.
-
That all makes sense and appreciate the responses. I will look into the proper way to get a graphics context noted by @DarranRowe. |
Beta Was this translation helpful? Give feedback.
-
you could use (IntPtr)AppWindow.Id.Value |
Beta Was this translation helpful? Give feedback.
Yes, this is by design.
Only Microsoft.UI.Xaml.Window is documented to implement IWindowNative. Basically, only a Window is backed by a Windows API HWND, UIElements aren't, they are more than likely backed by a composition visual element.
The intended way to get graphics content into Xaml is via SurfaceImageSource, VirtualSurfaceImageSource or SwapChainPanel.