{"record":{"id":"529da76f64521887","repo":"lepoco/wpfui","slug":"unable-to-determine-the-window-source","errorCode":null,"errorMessage":"Unable to determine the window source.","messagePattern":"Unable to determine the window source\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Appearance/ObservedWindow.cs","lineNumber":32,"sourceCode":"{\n    private readonly HwndSource _source;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ObservedWindow\"/> class.\n    /// </summary>\n    /// <param name=\"handle\">The handle of the window.</param>\n    /// <param name=\"backdrop\">The backdrop type of the window.</param>\n    /// <param name=\"updateAccents\">Indicates whether to update accents.</param>\n    public ObservedWindow(IntPtr handle, WindowBackdropType backdrop, bool updateAccents)\n    {\n        Handle = handle;\n        Backdrop = backdrop;\n        UpdateAccents = updateAccents;\n        HasHook = false;\n\n        HwndSource? windowSource = HwndSource.FromHwnd(handle);\n        _source =\n            windowSource ?? throw new InvalidOperationException(\"Unable to determine the window source.\");\n    }\n\n    /// <summary>\n    /// Gets the root visual of the window.\n    /// </summary>\n    public Window? RootVisual => (Window?)_source.RootVisual;\n\n    /// <summary>\n    /// Gets the handle of the window.\n    /// </summary>\n    public IntPtr Handle { get; }\n\n    /// <summary>\n    /// Gets the backdrop type of the window.\n    /// </summary>\n    public WindowBackdropType Backdrop { get; }\n\n    /// <summary>","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Appearance/ObservedWindow.cs#L14-L50","documentation":"Thrown in the ObservedWindow constructor when HwndSource.FromHwnd(handle) returns null, meaning the given HWND has no associated WPF HwndSource. The class caches the source to read RootVisual and attach hooks, so without it the watcher cannot operate on that window.","triggerScenarios":"SystemThemeWatcher.Observe/Watch is called with an IntPtr that is zero, stale, belongs to a non-WPF window, or whose HwndSource was already disposed. FromHwnd only resolves sources created by WPF and still alive.","commonSituations":"Passing a handle from a WinForms or native window, calling Watch after the window has been closed/disposed, or passing IntPtr.Zero because the window was not yet realized. Cross-process handles also return null.","solutions":["Only observe WPF System.Windows.Window instances and ensure they are loaded (IsLoaded true) before calling Watch/Observe so the HWND is valid.","Prefer the Window-based overloads of SystemThemeWatcher rather than constructing ObservedWindow with a raw handle.","If you must use a handle, confirm HwndSource.FromHwnd(handle) is non-null before constructing ObservedWindow."],"exampleFix":"// before\nvar obs = new ObservedWindow(someHandle, backdrop, true);\n\n// after\nif (HwndSource.FromHwnd(someHandle) is { } src)\n{\n    var obs = new ObservedWindow(someHandle, backdrop, true);\n}\nelse\n{\n    // handle is invalid or non-WPF; skip observing\n}","handlingStrategy":"validation","validationCode":"if (HwndSource.FromHwnd(handle) is null) { /* skip or log; do not construct ObservedWindow */ }","typeGuard":"static bool IsWpfHwnd(IntPtr h) => h != IntPtr.Zero && HwndSource.FromHwnd(h) is not null;","tryCatchPattern":null,"preventionTips":["Prefer Window-based SystemThemeWatcher overloads instead of raw HWND construction.","Verify the HWND belongs to a live WPF window before observing.","Avoid observing closed/disposed or cross-process windows."],"tags":["hwnd","interop","window","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}