Posted by: Zeeshan Amjad | June 16, 2011

Relationship between ICommand and RoutedCommand


RoutedCommand class in WPF implement the ICommand interface. But if we take a look at the methods of ICommand interface and RoutedCommand then it is slightly different. Here is a class diagram of ICommand interface.

ICommand

Here is a class diagram of RoutedCommand class.

RoutedCommandClassDiagram

Signature of CanExecute and Execute is different in ICommand interface and RoutedCommand. In fact this is  happing due to explicit interface implementation. Let’s take a look at simple example to demonstrate this concept. Suppose we have a very simple interface with only one method in it.

Code Snippet
public interface IMyInterface
{
    void DisplayMessage();
}

 

We are going to create one class that implement this interface explicitly. Not only that, but our class has one method that has same name but one parameter to display the given message. Here is our class.

Code Snippet
public class MyClass : IMyInterface
{
    void IMyInterface.DisplayMessage()
    {
        Console.WriteLine("IMyInterface.DisplayMessage");
    }

    public virtual void DisplayMessage(string message)
    {
        Console.WriteLine("MyClass.MyClassDisplayMessage " + message);
    }
}

Now we can create an object of this class, but we can’t use the interface method DisplayMessage without parameter. Here is the usage of our class.

Code Snippet
MyClass obj = new MyClass();

obj.DisplayMessage("Hello World");

 

For any reason if we want to call the interface implementation method directly, then we have to cast it into my interface then I will be able to use it. But in that case, we can’t call any other method of our class which is not present in the interface, such as in this example DisplayMessage with one parameter. Here is a piece of code to show this.

Code Snippet
IMyInterface myInterface = obj as IMyInterface;

if (myInterface != null)
{
    myInterface.DisplayMessage();
}

 

This will call the interface method implementation.

The same is true between ICommand and RoutedCommand class. RoutedCommand class implements ICommand interface explicitly, therefore we can’t use the ICommand methods directly. If we want to use the methods of ICommand interface due to any reason then we are suppose to cast the object of routed command into ICommand and then use it. Here is one simple example to show this.

Suppose we created one routed command. Here is a static object of RoutedCommand class.

Code Snippet
private static RoutedCommand routedCommand;

 

And here is a code to create routed command.

Code Snippet
InputGestureCollection inputs = new InputGestureCollection();
inputs.Add(new KeyGesture(Key.C, ModifierKeys.Alt, "Alt+C"));
routedCommand = new RoutedCommand("RoutedCommand", typeof(MyCommandClass), inputs);

 

Here is a code to use the Execute method of ICommand interface.

Code Snippet
ICommand command = routedCommand as ICommand;

if (command != null)
{
    command.Execute(null);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: