Exception when switching to Metro Dark

WPF Studio, Themes, and Shared Library for WPF Forum

The latest build of this product (v2020.1.1) was released 2 months ago, which was before this thread was created.
Posted 2 months ago by Martin de Jong - Blaise - Statistics Netherlands
Version: 19.1
Avatar

I launch a dialog, select the theme, return from the dialog, switch the theme and then:

if i had hidden the dialog it works fine

if i had closed the dialog it crashed

Seems a hidden windows gets the resource updated i guess.

------------------------------------------------------

Cannot set Owner property to a Window that has been closed.

StackTrace:
at System.Windows.Window.set_Owner(Window value)
at ActiproSoftware.Windows.Themes.WindowChromeShadow.AttachToPrimaryWindow(Window newPrimaryWindow)
at ActiproSoftware.Windows.Themes.WindowChromeShadow..ctor(Window primaryWindow)
at ActiproSoftware.Windows.Themes.WindowChromeManager.#5Lg()
at ActiproSoftware.Windows.Themes.WindowChromeManager.#6Lg(Boolean #BCf)
at ActiproSoftware.Windows.Themes.WindowChromeManager.#9Ej()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

[Modified 2 months ago]

Comments (3)

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Martin,

This scenario is where the outer glow for a WindowChrome (which has to be rendered in its own separate Window and used by our modern themes) is attaching to the WindowChrome'd Window as its owner.  But in your case, it sounds like the WindowChrome'd Window has already been closed.  I don't believe a WPF Window is intended to be reopened after it has been closed.  Most web articles will say that if you intend to reuse a Window, you should cancel the close and make it hidden instead.

But if you only showed a dialog and closed it and didn't reopen it, then we certainly don't want to see this exception happening.  If that is the case, can you put together a new simple sample project that shows this happening and send this to our support address so we can debug with that and see what we can do to fix it?  Please mention this thread in your e-mail and remove the bin/obj folders from the ZIP you send so it doesn't get spam blocked.  Thanks!


Actipro Software Support

Posted 2 months ago by Martin de Jong - Blaise - Statistics Netherlands
Avatar

I change the theme in a dialog window, which is immediately closed after this change. This code is quite old, but now in 19.1.687 it seems  broken. In our code i delayed the theme change to the dialog calling method, which solves this (little) problem.

In our earlier version(s) we still use 19.1.684, no sign of this wrinkle there. But in this dialog we use self made controls, so no way of predicting where the problem actually lies. 

Answer - Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Martin,

I was able to reproduce this scenario with this code if the app starts in an older theme like Aero that didn't use the outer glow:

protected override void OnKeyDown(KeyEventArgs e) {
	switch (e.Key) {
		case Key.F2:
			var window = new Window();
			WindowChrome.SetChrome(window, new WindowChrome());
			window.Closing += (s, e2) => {
				ThemeManager.CurrentTheme = ThemeName.MetroDark.ToString();
			};
			window.ShowDialog();
			break;
	}

	base.OnKeyDown(e);
}

That code shows a WindowChrome'd Window in response to F2.  Then when it's closing, it will switch the theme.

After testing, I can confirm it's a newer issue that started occurring in v19.1.0686 and related to a code update for this:

Updated how WindowChrome's shadow is tracked with its owner Window, which prevents the shadow from showing up on other virtual desktops

We're making several updates for the mext maintenance release that will prevent this bug.  First, we are now tracking if the WindowChrome'd Window's Closed event occurs and track a flag on if it's been closed.  We no longer create the outer glow if the Window has been closed.  Next, even if it the Window was closed before setting WindowChrome on it (unlikely this would ever happen, but would prevent our flag from detecting the Window was already closed), we put a try..catch around the Owner property set.  It's too bad the Window class doesn't have an IsClosed property on it.


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.