0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prism コードサンプル学習:10-CustomRegistrations

Last updated at Posted at 2021-05-24

Prism コードサンプル学習:10-CustomRegistrations

はじめに

以下の記事の続きです。
https://qiita.com/mngreen/items/5194c4620860b68c98b8

10-CustomRegistrations

本サンプルでは、前回に引き続き、Viewの型からViewModelを取得する際の解消方法を変更しています。
ただし、デフォルトの規則を変えるのではなく、追加して規則を登録しているという点が異なります。
サンプルの例では、ジェネリックな型を用いたパターンで登録していますが、コメントアウトの登録方法でも可能なようです。

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        ...

        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();

            // type / type
            //ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), typeof(CustomViewModel));

            // type / factory
            //ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), () => Container.Resolve<CustomViewModel>());

            // generic factory
            //ViewModelLocationProvider.Register<MainWindow>(() => Container.Resolve<CustomViewModel>());

            // generic type
            ViewModelLocationProvider.Register<MainWindow, CustomViewModel>();
        }
    }
  • 上記の4つの方法ではそれぞれ以下のメソッドと対応します。
  • これら4つの方法はファクトリーを登録するか、タイプを登録するかで登録方法が2つに分かれます。オーバーロードによって実現されています。
    • ファクトリーを登録:type / factory, generic factory
    • タイプを登録:type / type, generic type

おわりに

今回はViewModelLocatorの型の解消の変更方法について、別途登録する方法を学習できました。
フレームワークとして様々な登録方法が用意されているのは勉強になります。
自分であれば、いずれかの方法で登録できれば良いと考えてしまいそうなので。。
次回、11-UsingDelegateCommandsについて見ていこうと思います。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?