3
1

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 コードサンプル学習:11-UsingDelegateCommands

Posted at

Prism コードサンプル学習:11-UsingDelegateCommands

はじめに

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

11-UsingDelegateCommands

本サンプルでは、DelegateCommandsの実装方法を紹介しています。
Prismフレームワークでは、以下の4つの方法でコマンドが生成されています。

    public class MainWindowViewModel : BindableBase
    {
        ...

        public MainWindowViewModel()
        {
            // コマンド生成:①
            ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);

            // コマンド生成:②
            DelegateCommandObservesProperty = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => IsEnabled);

            // コマンド生成:③
            DelegateCommandObservesCanExecute = new DelegateCommand(Execute).ObservesCanExecute(() => IsEnabled);

            // コマンド生成:④
            ExecuteGenericDelegateCommand = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);
        }

        ...
    }
  • コマンド生成:①
    • 一番シンプルなコマンドで、実行する引数なしアクションと実行できるかを判定する関数を渡している
    • プロパティ変更通知が飛ばないため、IsEnabledプロパティのセッターの末尾でプロパティ変更を通知するメソッドの呼び出しが必要
  • コマンド生成:②
    • コマンド生成:①を呼出し後にObservesProperty()メソッドを呼び出している
    • プロパティ変更通知対象をFuncで指定するパターンで、プロパティ変更通知メソッドの呼び出しを代わりにしてくれる
    • プロパティ変更通知メソッドの呼び出しの代わりはPropertyObserverクラスで行っている
      • 実際にPropertyChangedイベントにハンドラを登録しているのはここで行われていることを確認できる。次のノードへの参照があることで、登録されているノードを次々と更新できる。
  • コマンド生成:③
  • コマンド生成:④
    • 上記でコマンド生成後に、ObservesCanExecute()を呼び出し
    • Genericで指定できる以外は③と変わらないように見える

おわりに

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

3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?