Prism コードサンプル学習:09-ChangeConvention
はじめに
以下の記事の続きです。
https://qiita.com/mngreen/items/749a57635cffbb146646
09-ChangeConvention
本サンプルでは、Viewの型からViewModelを取得する際の解消方法を変更しています。
デフォルトの規則ではViewModelはViewModelパッケージ以下に格納されなければ型解消できないのですが、本サンプルでは、Viewパッケージ以下に格納されているにもかかわらずViewModelが生成されています。
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
...
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
{
var viewName = viewType.FullName;
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";
return Type.GetType(viewModelName);
});
}
}
-
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver内でデフォルトのResolverを置き換えています。
- 元々の規則は45行目にありました。
- ちなみに ConfigureViewModelLocator メソッドはInitializeInternalメソッド内で呼び出されています。
おわりに
今回はViewModelLocatorの型の解消の変更方法について学習できました。
基本的に自前で変更したいというモチベーションがなければ利用しないかもしれませんが、カスタマイズ性を持たせている点は設計として優れているように感じました。
次回、10-ChangeConventionについて見ていこうと思います。