Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Using Commands

Using Commands Silverlight for Windows Phone supports the ICommand interface for buttons and various other controls. Commands are useful because when exposed from a viewmodel they allow your view to bind to them just like other properties; when the user interacts with the visual element, the command is executed. This enables you to move your UI logic from event handlers to higher level classes. The ICommand interface defines the following three members: • CanExecute(object)—A method called by the Silverlight commanding infrastructure, which automatically sets the enabled state of the target control • Execute(object)—A method that performs the logic of the command • CanExecuteChanged—An event that signals that the Silverlight commanding infrastructure should reevaluate the executable state of the command by calling its CanExecute method Within the downloadable sample code there is a default implementation of the ICommand interface called DelegateCommand<T>. This class has features such as object parameter type coercion, which, for example, enables you to use strings to represent enum values in binding expressions, which are automatically converted to the appropriate enum type. In this book you commonly see commands defined as read only fields exposed using a property get accessor, as this excerpt from the MediaViewModel in Chapter 7, “Media and Web Elements,” shows: readonly DelegateCommand playCommand;

public ICommand PlayCommand
{
    get
    {
        return playCommand;
    }
} Most often you see commands instantiated in the viewmodels constructor. The DelegateCommand constructor accepts an Action argument, which is carried out when the command is executed. In the following excerpt you see the instantiation of a command called playCommand that when executed sets a number of viewmodel properties: public MediaViewModel()
{
    playCommand = new DelegateCommand(
        obj =>
        {
                PlayerState = PlayerState.Playing;
                CanPlay = false;
                CanPause = true;
        });
...
} DelegateCommand along with its generic counterpart DelegateCommand<T> also allow you to specify an Action that is used to evaluate whether the command is able to be executed. Ordinarily the Silverlight commanding infrastructure is only supported on buttons (ButtonBase) and a couple of specialized controls. Some extra capabilities are provided in the ICommand implementation that allow you to wire the command to any FrameworkElement, such as in the following example, which shows a Silverlight Image element that when tapped causes an ICommand to be executed: <Image Source="/Foo.png"
        c:Commanding.Command="{Binding ViewCommand}"
        c:Commanding.CommandParameter="{Binding FullScreen}" /> Note The event used to trigger command execution can be specified by using the Commanding.Event attached property. In subsequent chapters you see several examples of using these custom commanding attached properties.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint