WindowWrapper throwing exception

Topics: Prism v4 - WPF 4
Jun 30, 2011 at 9:01 PM

I have a popup region that has a view with a child view. The child view has the WindowCloseBehavior.

There is a close menu item on my parent view that removes the child and parent view from the popup region.

Whenever I remove or deactivate the parent view from the popup region, the ActiveViews_CollectionChanged method is called and calls the CloseContentDialog method. When the CloseContentDialog tries to set the Owner to null I get a “Cannot set Owner property after Dialog is shown.” exception.

If I click the x button on the window everything works properly.

Any ideas on what I am doing wrong?

Here is the stack trace if it helps.

   at System.Windows.Window.set_Owner(Window value)
   at Framework.WindowWrapper.set_Owner(Object value) in D:\Projects\Framework\Framework\Behaviors\WindowWrapper.Desktop.cs:line 83
   at Framework.DialogActivationBehavior.CloseContentDialog() in D:\Projects\Framework\Framework\Behaviors\DialogActivationBehavior.cs:line 145
   at Framework.DialogActivationBehavior.ActiveViews_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) in D:\Projects\Framework\Framework\Behaviors\DialogActivationBehavior.cs:line 108
   at Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyRemove(IList items, Int32 originalIndex)
   at Microsoft.Practices.Prism.Regions.ViewsCollection.RemoveFromFilteredList(Object item)
   at Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
   at System.Collections.ObjectModel.Collection`1.Remove(T item)
   at Microsoft.Practices.Prism.Regions.Region.Remove(Object view)
   at Company.ModuleController.RemoveView(String regionName, String viewName) in D:\Projects\Presentation\Company\Shell\Company\Controllers\ModuleController.cs:line 122
   at Company.Modules.Core.ViewModels.DetailViewModel.CloseView() in D:\Projects\Presentation\Company\Modules\Company.Modules.Core\ViewModels\Settings\Settings\DetailViewModel.cs:line 405
   at Company.Modules.Core.ViewModels.DetailViewModel.ExecuteCloseCommand(Object parameter) in D:\Projects\Presentation\Company\Modules\Company.Modules.Core\ViewModels\Settings\Settings\DetailViewModel.cs:line 153
   at Framework.SimpleCommand`2.Execute(T2 parameter) in D:\Projects\Framework\Framework\Commands\SimpleCommand.cs:line 138
   at Framework.SimpleCommand`2.Execute(Object parameter) in D:\Projects\Framework\Framework\Commands\SimpleCommand.cs:line 167
   at Microsoft.Practices.Prism.Commands.CompositeCommand.Execute(Object parameter)
   at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
   at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()

Jul 1, 2011 at 7:53 PM

Hi,

It would be helpful if you could provide us with a repro sample that demonstrates this problem, so that we can help you find the cause for it.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Jul 5, 2011 at 5:38 PM

I figured out what the problem was.
My shell is not a window, it is a user control. I have a “ShellContainer” that holds the shell. This allows us to have different shells depending on the theme.

In the PrepareContentDialog() method, I needed to change the Owner to: this.contentDialog.Owner = this.HostControl.GetParentObject();

I was also setting the dialog’s value to false after the view was removed, which is why I was getting the “Cannot set Owner property after Dialog is shown”

Developer
Jul 5, 2011 at 5:41 PM

Hi,

I'm glad that you've found a solution to your problem. Thank you for sharing it with the rest of the community.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi