Life is Really Short, Have Your Life!!

ござ先輩の主に技術的なメモ

PrismのDialogServiceでDIしたViewModelが毎回初期化される

PrismのDialogServiceでDIしたViewModelに限って、毎回ViewModelのコンストラクタが呼ばれるという現象がありました。

prism:ViewModelLocator.AutoWireViewModel="True"にしているのにね。この設定にした場合、ViewModelのインスタンスはSingletonになってくれるが、DialogServiceで呼び出す小画面に限りなぜか・・・

  • バージョン: Prism.Wpf 7.2.0.1422

結論から言えば、このコードを書けばSingletonになってくれた。

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
      containerRegistry.RegisterSingleton<OrdeSearchPageViewModel>();
}

RegisterSingleton で、型に対象のVMのクラスを指定するだけだった。Prismの沼かもしれん。