Q: A window event gives me a window ID. How do I get back the view object I registered?
If you register your own view (for example, a WinForms or WPF window) with RegisterAppFactoryAsync or RegisterInstance, it becomes an io.Connect window with an ID. Window events, such as focus changes, give you that ID. You often need the opposite: the original view object.
Find the window
To find any io.Connect window by ID, use GetGDWindows():
var targetWindowId = "29476_0";
IGDWindow[] allWindows = await io.GlueWindows.GetGDWindows();
IGDWindow targetWindow = allWindows.FirstOrDefault(window => window.Descriptor.Id == targetWindowId);
To search only your own app’s windows, use GlueWindows.OwnWindows (io.Connect.NET 1.36.0 or later).
Get the view (io.Connect.NET 1.37.0 or later)
From 1.37.0, the library stores the view you passed at registration on the window, under StickyWindow.View. Cast it to the type you registered:
glue_.GlueWindows.SubscribeEvents<WindowFocusChangedEvent>(((@event, descriptor) =>
{
if (glue_.GlueWindows.OwnWindows.FirstOrDefault(gdw => gdw.Id == descriptor.Id) is { } wnd)
{
var view = wnd.StickyWindow.View as Window;
Debug.WriteLine($"{(@event.Data ? "got" : "lost")} focus: {@event.WindowId} with host {descriptor.ApplicationName}");
}
}));
References
- Window Management (.NET): io.Connect Desktop Documentation - Windows > Window Management > .NET
- App Management (.NET): io.Connect Desktop Documentation - App Management > .NET