stride3d/stride · error · InvalidOperationException
Unable to retrieve the HwndSource of this window
Error message
Unable to retrieve the HwndSource of this window
What it means
DragWindow.InitializeExtendedStyle needs the Win32 HwndSource of the WPF Window to apply WS_EX_TRANSPARENT/WS_EX_LAYERED/WS_EX_TOPMOST styles. PresentationSource.FromVisual(this) returns null when the window has no presentation source yet (not fully shown/loaded into a HWND), so an InvalidOperationException is thrown.
Solutions
- Hook InitializeExtendedStyle to the SourceInitialized event instead of Loaded/constructor
- Guard with a null check on PresentationSource.FromVisual and defer/retry once the window is shown
- Ensure Show()/ShowDialog() is actually called and the window is not being manipulated after Close()
Example fix
// before
public DragWindow() { InitializeExtendedStyle(null, null); }
// after
public DragWindow() { SourceInitialized += InitializeExtendedStyle; } Defensive patterns
Strategy: validation
Validate before calling
var source = PresentationSource.FromVisual(dragWindow); if (source is HwndSource hwndSource) InitializeStyle(hwndSource.Handle); else dragWindow.SourceInitialized += (s,e) => InitializeExtendedStyle(s, e);
Type guard
static bool TryGetHwndSource(Visual v, out HwndSource src) => (src = PresentationSource.FromVisual(v) as HwndSource) != null;
Try / catch
try { InitializeExtendedStyle(sender, e); }
catch (InvalidOperationException) { window.SourceInitialized += InitializeExtendedStyle; } // defer until HWND exists Prevention
- Always apply Win32 window styles from the SourceInitialized event, not the constructor
- Never manipulate HWND-based state after Window.Close()
- Guard PresentationSource.FromVisual results for null
When it happens
Trigger: Calling InitializeExtendedStyle (e.g. from a SourceInitialized/Loaded handler wired too early, or constructor) before the window's HWND exists, or after the window was closed.
Common situations: Subscribing the handler in the Window constructor instead of waiting for SourceInitialized; showing the window in a non-WPF-hosted context where no HwndSource is created; calling on a Window that never got shown.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- A dependency object is already attached to this instance of…
- A dependency object must be attached in order to register a…
- A dependency object must be attached in order to unregister…
- A dispatcher lock must be created from a different thread…
- A part named ' ' must be present in the ControlTemplate…
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/178680855cd5a6e7.
Report an issue: GitHub.
Appendix: source
Thrown at sources/editor/Stride.Core.Assets.Editor/View/Behaviors/DragDrop/DragWindow.cs:31
{
AllowDrop = false;
AllowsTransparency = true;
Background = null;
IsHitTestVisible = false;
ShowActivated = false;
ShowInTaskbar = false;
SizeToContent = SizeToContent.WidthAndHeight;
Style = null;
Topmost = true;
WindowStyle = WindowStyle.None;
SourceInitialized += InitializeExtendedStyle;
}
private void InitializeExtendedStyle(object sender, EventArgs eventArgs)
{
var source = (HwndSource)PresentationSource.FromVisual(this);
if (source == null)
throw new InvalidOperationException("Unable to retrieve the HwndSource of this window");
int extendedStyle = NativeHelper.GetWindowLong(source.Handle, NativeHelper.GWL_EXSTYLE);
extendedStyle = (int)(extendedStyle | NativeHelper.WS_EX_TRANSPARENT | NativeHelper.WS_EX_LAYERED | NativeHelper.WS_EX_TOPMOST);
NativeHelper.SetWindowLong(source.Handle, NativeHelper.GWL_EXSTYLE, extendedStyle);
}
}
}
View on GitHub (pinned to 96fad776d2)