Getting a custom Panel in Rhino 6 (Rhino 5 method no longer works)

I’m updating a Rhino 5 plugin to work in Rhino 6. As part of that, I’m using a custom panel in Rhino as a host for some WPF controls. The following code worked for this in Rhino 5:

using RUI = Rhino.UI;
using R = Rhino;

            public void CreateHostDockPanel()
                if (!RUI.Panels.IsPanelVisible(typeof(SideBarUIHost).GUID))
                    if (!RUI.Panels.OpenPanelAsSibling(typeof(SideBarUIHost).GUID, RUI.PanelIds.Layers))

                SideBarControl selectPanel = new SideBarControl();
                selectPanel.DataContext = Core.Instance;
                SideBarUIHost host = (SideBarUIHost)RUI.Panels.GetPanel(typeof(SideBarUIHost).GUID);

In Rhino 6, however, this causes a problem because while it creates the panel OK, trying to retrieve it with the second-to-last line returns null, unless I create the panel, manually click on it to make it topmost and then re-run the code, in which case it retrieves the SideBarUIHost panel successfully.

Looking in the docs, Panels.GetPanel(Guid) is marked as deprecated. Fair enough; there are quite a few overloads which are not so I tried switching to some of them. I’ve tried replacing that line with all of:

SideBarUIHost host = (SideBarUIHost)RUI.Panels.GetPanel(typeof(SideBarUIHost).GUID);

SideBarUIHost host = (SideBarUIHost)RUI.Panels.GetPanel(typeof(SideBarUIHost).GUID, R.RhinoDoc.ActiveDoc);

object[] panels = RUI.Panels.GetPanels(typeof(SideBarUIHost).GUID, R.RhinoDoc.ActiveDoc);
SideBarUIHost host = panels.FirstOrDefault() as SideBarUIHost;

SideBarUIHost host = RUI.Panels.GetPanel<SideBarUIHost>(R.RhinoDoc.ActiveDoc);

These are not marked as obsolete, but they all display the same behaviour, returning null or an empty array unless I first manually give the panel focus.

Is this expected behaviour?

What is the correct way of accessing created custom panels in Rhino 6?

For the benefit of anybody else experiencing the same problem: I’ve managed to find a partial solution. The OpenPanelAsSibling and OpenPanel functions both have an override that also takes a boolean - when set to true this makes those panels foremost and this then means that GetPanel functions OK. Possibly the actual change from Rhino 5 is that created panels are no longer foremost by default.

Still, it’s troubling that GetPanel is not more robust and I can envision scenarios where I will require access to a custom panel that doesn’t currently have user focus, so if anybody has a solution to the question as-asked I’d be very interested.

Rhino 6 does not create an instance of a registered panel class until the first time the panel is actually displayed which is why passing true to the make current argument causes an instance of the panel to get created/found. It is possible to add a panel tab to a container without ever creating an instance of the panel if the panel tab never actually becomes current.