,

Using Commands

Windows Phone XAML apps support 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:

Image CanExecute(object)A method called by the commanding infrastructure, which automatically sets the enabled state of the target control

Image Execute(object)A method that performs the logic of the command

Image CanExecuteChangedAn event that signals that the 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, “Employing 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 invoked 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 built-in commanding infrastructure is supported only 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 an 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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.213.196