WPF、.Net 4、Prism 4.1、およびUnityを使用してPrismアプリケーションを構築しました。DirectoryModuleCatalogを使用して、実行時にモジュールを検索しています。ビューはTabControl(MainRegion)に表示されます。リージョンからビューを削除すると、ビューとビューモデルはメモリに残り、ガベージコレクションは行われません。タブアイテムは削除されます。何時間も検索した後、私は自分が間違っていることを理解できません。
これが私のブートストラッパーです:
public class Bootstrapper : UnityBootstrapper
{
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
protected override DependencyObject CreateShell()
{
var shell = new Shell();
return shell;
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
}
}
これが私のモジュールです:
[Module(ModuleName = "ModuleA")]
public class Module : IModule
{
private IRegionManager _regionManager;
public Module(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
var view = new UserControl1();
//_regionManager.RegisterViewWithRegion("MainRegion", typeof(UserControl1));
_regionManager.Regions["MainRegion"].Add(view, "ModuleA");
_regionManager.Regions["MainRegion"].Activate(view);
}
}
そして、リージョンに追加される私のビューのビューモデルは次のとおりです。
public class ViewModel
{
public DelegateCommand RemoveView { get; set; }
public ViewModel()
{
RemoveView = new DelegateCommand(() =>
{
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var view = regionManager.Regions["MainRegion"].GetView("ModuleA");
regionManager.Regions["MainRegion"].Deactivate(view);
regionManager.Regions["MainRegion"].Remove(view);
});
}
}
そして、これがビューの背後にあるコードです:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
モジュール内のビューまたはおそらくビュー内のビューモデルをインスタンス化していることが原因である可能性があることを読みました。Red Gate Memory Profilerを使用し、DelegateCommandを使用してビューを削除すると、ビューとビューモデルの両方に、ガベージコレクションができないというフラグが立てられます。私が適切にカットしていないという参照はどこにありますか?
Antsの保持グラフは次のとおりです:https ://docs.google.com/file/d/0B4XjO9pUQxBXbGFHS1luNUtyOTg/edit?usp = Sharing
これが問題を示すテストソリューションです。
また、 CodePlexにも質問を投稿しました。