かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[WPF][C#]Composite Application Guidance for WPFのコマンド(DelegateCommand<T>, CompositeCommand)

Composite Application Guidance for WPFには、ICommandの実装クラスとしてDelegateCommand<T>とCompositeCommandというクラスが用意されている。この2つのCommandについてちょっと見てみようと思う。

DelegateCommand<T>

このクラスは、すこぶるシンプル。
コンストラクタは以下の二種類ある。

  1. DelegateCommand<T>(Action<T> executeMethod)
    DelegateCommand<T>.Execute(T parameter)が呼び出されたときに呼ばれるメソッドを指定するコンストラクタ
  2. DelegateCommand<T>(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
    DelegateCommand<T>.Execute(T parameter)が呼び出されたときに呼ばれるメソッドと、DelegateCommand<T>.CanExecute(T parameter)が呼び出されたときに呼ばれるメソッドを指定するコンストラクタ

基本このコンストラクタを使うだけでよさそうだ。ExecuteやCanExecuteを直接呼ぶことは少ないだろうからね。ちょいとお試し。

using System;
using Microsoft.Practices.Composite.Wpf.Commands;

namespace CompositeCommandTest
{
    class Program
    {
        // Executeが呼ばれたときに呼ばれる
        private static void ExecuteCallback(string parameter)
        {
            Console.WriteLine("Execute: " + parameter);
        }

        // CanExecuteが呼ばれたときに呼ばれる
        private static bool CanExecuteCallback(string parameter)
        {
            Console.WriteLine("CanExecute: " + parameter);
            return true;
        }

        static void Main(string[] args)
        {
            // コマンド作ってCanExecuteとExecuteを呼んでみる
            var command = new DelegateCommand<string>(ExecuteCallback, CanExecuteCallback);
            command.CanExecute("ほげ");
            command.Execute("もげ");
        }
    }
}

実行結果は以下の通り。

CanExecute: ほげ
Execute: もげ

まぁ動きとしては問題ない。

CompositeCommand

これもシンプル構造のCommandになってる。どういうCommandかというと、複数のCommandを1まとめにするというもの。
CompositeCommand.RegisterCommand(ICommand command)メソッドでCommandを登録して、CompositeCommand.UnregisterCommand(ICommand command)メソッドで登録を取り消す。

Commandを登録しておくと、ExecuteやCanExecuteを呼び出したときにすべてのCommandが実行されるようになる。CanExecuteはすべてのCommandのCanExecuteがtrueの場合にtrueを返すようだ。というわけで早速実験!!

using System;
using Microsoft.Practices.Composite.Wpf.Commands;

namespace CompositeCommandTest
{
    class Program
    {

        static void Main(string[] args)
        {
            // CompositeCommandを用意して
            var compositeCommand = new CompositeCommand();

            // ExecuteがConsole.WriteLineで、CanExecuteが常にtrueのコマンドを作って登録
            var command1 = new DelegateCommand<object>(
                Console.WriteLine, (arg) => true);
            compositeCommand.RegisterCommand(command1);

            // CanExecuteとExecuteの実行
            Console.WriteLine("CanExecuteの実行結果: " + compositeCommand.CanExecute(null));
            compositeCommand.Execute("はろーわーるど1");

            // Commandを2つ登録する前と後の実行結果を区切るための目印
            Console.WriteLine("----------------------------------");

            // ExecuteがConsole.WriteLineで、CanExecuteが常にfalseのコマンドを作って登録
            var command2 = new DelegateCommand<object>(
                Console.WriteLine, (arg) => false);
            compositeCommand.RegisterCommand(command2);

            // CanExecuteとExecuteの実行
            Console.WriteLine("CanExecuteの実行結果: " + compositeCommand.CanExecute(null));
            compositeCommand.Execute("はろーわーるど2");
        }
    }
}

実行結果は以下のようになる。

CanExecuteの実行結果: True
はろーわーるど1
----------------------------------
CanExecuteの実行結果: False
はろーわーるど2
はろーわーるど2

動きとしては問題ない。

何のためにあるの?

WPFには、デフォルトでICommandの実装としてRoutedCommandやRoutedUICommandっていうクラスが用意されてて、CommandBindingとかと組み合わせると、快適に実装できるようになっている。
にも関わらず、Composite Application Guidance for WPFで新たに2つの実装を用意してるのは何でだろう?ということになってくる。これは、Composite Application Guidance for WPFで推奨するアプリの実装方法のPresentationModelパターンでの実装を視野に入れているからっぽい。

PresentationModelパターンは、View(WPFでいうと恐らくxaml + コードビハインドクラス)の他に、PresentationModelというView用のプロパティや処理を受け持つクラスを作るということになっている。
ということは、ボタンのクリック処理なんかもPresentationModel側に書くのがセオリーってことになってくるが、RoutedCommandは、Visual Tree上のものにしか、ExecuteやCanExecuteを関連付けることを、あまり意識して作られてなさげ。
なので、シンプルにdelegateを使ったDelegateCommand<T>とかが出来たんだと思われる。

CompositeCommandは、単純に複数のCommandを1つにまとめたかっただけかもしれん。英語難しい…。

投稿日時 : 2008年10月20日 0:33

Feedback

# welded ball valve 2012/10/19 0:48 http://www.jonloovalve.com/Full-welded-ball-valve-

Only a smiling visitant here to share the love (:, btw outstanding design. "Treat the other man's faith gently it is all he has to believe with." by Athenus.

# louis vuitton outlet 2012/10/28 3:10 http://www.louisvuittonbackpack2013.com/

Enjoy is usually weak at just entry into the world, having said that it becomes more substantial as we age if it's as it should be provided.
louis vuitton outlet http://www.louisvuittonbackpack2013.com/

# cheap louis vuitton purses 2012/10/28 3:10 http://www.louisvuittonoutletbags2013.com/

Father‘l test overtime, the best elements may be purchased when you smallest assume all of them.
cheap louis vuitton purses http://www.louisvuittonoutletbags2013.com/

# Burberry Watches 2012/10/28 18:25 http://www.burberryoutletscarfsale.com/accessories

You have brought up a very good details , regards for the post.
Burberry Watches http://www.burberryoutletscarfsale.com/accessories/burberry-watches.html

# If you would like to take a great deal from this piece of writing then you have to apply such techniques to your won webpage. 2019/05/12 12:50 If you would like to take a great deal from this p

If you would like to take a great deal from this piece of writing then you have to apply such techniques to your won webpage.

# My developer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on several websites for about a year and am nervous about switching to another 2019/05/17 20:48 My developer is trying to convince me to move to .

My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs. But he's tryiong none the less.
I've been using WordPress on several websites for about
a year and am nervous about switching to another platform.
I have heard excellent things about blogengine.net. Is
there a way I can transfer all my wordpress posts into it?

Any help would be really appreciated!

# Hello, after reading this amazing piece of writing i am as well happy to share my know-how here with mates. 2019/06/04 23:46 Hello, after reading this amazing piece of writing

Hello, after reading this amazing piece of writing i
am as well happy to share my know-how here with mates.

# Spot on with this write-up, I seriously believe that this site needs much more attention. I'll probably be back again to read more, thanks for the advice! 2019/06/06 22:36 Spot on with this write-up, I seriously believe th

Spot on with this write-up, I seriously believe that this site needs much more attention. I'll probably
be back again to read more, thanks for the advice!

# Нelⅼo, i feel that i saw you visited my weblog so і got here tо rdturn thе prefer?.I'm attempting to ffind issues to improve my site!I suppose іts ɡood enouցh to makе use οf a feᴡ of ʏouг ideas!! 2019/09/15 9:55 Heⅼlo, і feel tһat і sɑw youu visited mу weblog ѕ

Hello, i feel thаt i saw you visited mmy weblog ?o i got ?ere to return the prefer?.Ι'm attempting
to find issues tο improve my site!I supposse its goоd enoiugh to ake ?sе of а few of
your ideas!!

# re: [WPF][C#]Composite Application Guidance for WPF?????(DelegateCommand&lt;T&gt;, CompositeCommand) 2021/07/16 15:52 hydroxychoroquine

choroquine https://chloroquineorigin.com/# side effects of hydroxychlor 200 mg

# re: [WPF][C#]Composite Application Guidance for WPF?????(DelegateCommand&lt;T&gt;, CompositeCommand) 2021/07/26 4:55 chloroquine phosphate vs hydroxychloroquine sulfat

chloroquina https://chloroquineorigin.com/# does hydroxychloroquine have side effects

# re: [WPF][C#]Composite Application Guidance for WPF?????(DelegateCommand&lt;T&gt;, CompositeCommand) 2021/08/08 19:00 hydroxychlor tab

is chloroquine available over the counter https://chloroquineorigin.com/# hydroxychloronique

# This article presents clear idea designed for the new users of blogging, that really how to do blogging and site-building. 2022/03/23 16:54 This article presents clear idea designed for the

This article presents clear idea designed for the new users of blogging, that really how to
do blogging and site-building.

# This article presents clear idea designed for the new users of blogging, that really how to do blogging and site-building. 2022/03/23 16:55 This article presents clear idea designed for the

This article presents clear idea designed for the new users of blogging, that really how to
do blogging and site-building.

# This article presents clear idea designed for the new users of blogging, that really how to do blogging and site-building. 2022/03/23 16:56 This article presents clear idea designed for the

This article presents clear idea designed for the new users of blogging, that really how to
do blogging and site-building.

# This article presents clear idea designed for the new users of blogging, that really how to do blogging and site-building. 2022/03/23 16:57 This article presents clear idea designed for the

This article presents clear idea designed for the new users of blogging, that really how to
do blogging and site-building.

# I think this is among the most vital info for me. And i'm glad reading your article. But should remark on some general things, The website style is ideal, the articles is really great : D. Good job, cheers 2022/03/24 5:09 I think this is among the most vital info for me.

I think this is among the most vital info for me.
And i'm glad reading your article. But should remark on some
general things, The website style is ideal, the articles is really great
: D. Good job, cheers

# jbwthqbsihmb 2022/05/26 17:09 qoygmgnm

https://erythromycin1m.com/# uses for erythromycin ophthalmic ointment

# Test, just a test 2022/12/16 0:28 http://candipharm.com

canadian generic pills http://candipharm.com

# hydroxychloroquine tablets 2022/12/28 15:19 MorrisReaks

order plaquenil pill http://www.hydroxychloroquinex.com/#

タイトル
名前
Url
コメント