0
votes

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.

1
you could use the new IDialogService that Prism offers github.com/PrismLibrary/Prism/pull/1682 - Luca
I think IDialogService is mainly used to replace MessageBox use cases. In my case, I have a pretty complex UI on the WPF with 3 regions in a shell. - Scorp
You can combine the IDialogService using your own ViewModel, Regions etc and use a scoped region manager. Otherwise you would need to create a ShellService --> There is an answer for that already: stackoverflow.com/questions/33010756/… - Luca

1 Answers

1
votes

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.