How to dispose View and Viewmodel with CreationPolicy.NonShared

Topics: Prism v4 - WPF 4
Jun 17, 2015 at 2:35 PM
Hi All,

I am building a wpf app with prism 4.0 as well as MEF.

Sample code like below:

View:

[Export(ViewNames.ModuleBuildingApprovalView,typeof(UserControl))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class ModuleBuildingApprovalView : UserControl, IView, INavigationAware
ViewModel:
[Export(typeof(ModuleBuildingApprovalViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ModuleBuildingApprovalViewModel : BaseViewModel
The problem is each time the view is opened, it's not disposed by GC after it's closed, that may cause big memory leak. Any good way to dispose the view and viewmodel manaully in code?
Jun 17, 2015 at 2:41 PM
By the way, i searched on website and found a couple of discussion related, like below two:

http://mansoormehmood.blogspot.com/2010/06/memory-leak-removing-view-with-child.html
http://toreaurstad.blogspot.no/2013/07/mef-and-memory-in-mef-2.html

However, my view and viewmodel are not disposed anyway even i use above two solutions, my view and viewmode is opened as child region of main app container, meanwhile they contains a series of child view/viewmodels. could anyone help me out? thanks in advance.