Scorp
Scorp

Reputation: 247

Removing ViewModels in Prism when Closing Window

I have a winform application with data grid rows with some icons. When the user clicks on one of the icons, a WPF window opens. I have created this WPF window using Prism i.e. it has shell and regions mapped to view.

The issues I am facing is: When I tried to close the WPF window, I get the exception "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed." I understand that we can resolve the issue by hiding the window instead of closing it. However, this makes my ViewModel and Services representing the older WPF window. I have kept the static counter in the ViewModels and observed that every time, I open the WPF window, static count increases which means my old view models are not getting destroyed.

I would like how to handle this scenario correctly so that when I close the window everything related to the window should be disposed off. I tried to do container.dispose in ShellViewModel, however, still it did not work.

Upvotes: 0

Views: 991

Answers (1)

Haukinger
Haukinger

Reputation: 10863

There are two aspects here. Firstly, you can use either RegionMemberLifetimeAttribute on your view model or implement IRegionMemberLifetime to make Prism create a new instance each time.

Secondly, you have to create your own RegionBehavior (or take it from this Github Issue) to make Prism dispose view models.

Upvotes: 1

Related Questions