【Prism】MEF版Commanding

引言

     接下来的是Commanding Demo的改造.

DelegateCommand

   WPF本身提供了一个RoutedCommand,然而没什么卵用.在Prism框架中提供了个更人性化的ICommand的实现--DelegateCommand,如下

public class ArticleViewModel : NotificationObject

{

    private readonly ICommand showArticleListCommand;



    public ArticleViewModel(INewsFeedService newsFeedService,

                            IRegionManager regionManager,

                            IEventAggregator eventAggregator)

    {

        this.showArticleListCommand = new DelegateCommand(this.ShowArticleList);

    }



    public ICommand ShowArticleListCommand 

    {

        get { return this.showArticleListCommand; } 

    }

}

CompositeCommand

    CompositeCommand是Prism提供的一个组合式命令实现.它可以在子命令都可用的情况下同时执行多个子命令.应用情况不是很多,但是可以了解一下.如下

public class MyViewModel : NotificationObject

{

    private readonly CompositeCommand saveAllCommand;



    public ArticleViewModel(INewsFeedService newsFeedService,

                            IRegionManager regionManager,

                            IEventAggregator eventAggregator)

    {

        this.saveAllCommand = new CompositeCommand();

        this.saveAllCommand.RegisterCommand(new SaveProductsCommand());

        this.saveAllCommand.RegisterCommand(new SaveOrdersCommand());

    }



    public ICommand SaveAllCommand

    {

        get { return this.saveAllCommand; }

    }

}

示例源码

       http://pan.baidu.com/s/1sjuWkod

小结

     更多的Commandi用法可以在官方文档Prism 4.0的Chapter 9中查阅.

 

你可能感兴趣的:(command)