Q2 and Q3 releases brought up support for commands usable in a similar way as the commanding mechanism from WPF.

In a few points the Commanding mechanism provides several classes:

  • RoutedCommand and RoutedUICommand – ICommand implementations, the second has a Text property
  • ICommandSource – once an object implement it, it knows how to invoke a command, i.e. it receives Command, CommandParameter and CommandTarget properties.
  • CommandBinding – maps the command logic to the command
  • CommandManager – provides methods that register CommandBinding and InputBinding objects, add and remove command event handlers, and provides services for querying the status of a command.

Creating a command

Basically, we can implement the ICommand interface in order to create our command class. However, we can also use the provided RoutedUICommand(or RoutedCommand) just as we do in WPF:

public static class DateTimeCommands
{
private static RoutedUICommand myCommand = new RoutedUICommand("ShowLocalTime", "ShowLocalTimeCommand", typeof(DateTimeCommands));

public static RoutedUICommand ShowLocalTimeCommand
{
get { return myCommand; }
}    

In order to use our new command, we need to register a CommandBinding in the class’ static constructor where we would like to use it:

static MainPage()
{ 
CommandManager.RegisterClassCommandBinding(typeof(MainPage), new CommandBinding(DateTimeCommands.ShowLocalTimeCommand, OnShowLocalTimeCommand, OnQueryShowLocalTimeCommand));
}
 
As seen this involves specifying handlers where we implement our logic which is executed upon invocation of the command. If we want to disable execution, all we need to do is set the CanExecute property of CanExecuteRoutedEventArgs to false:
private static void OnQueryShowLocalTimeCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
Apart from CanExecute, we have access to the Parameter property which holds the CommandParameter passed when this property is set to the command source. This is also accessible in the ExecutedRoutedEventHandler’s event args and can be used to implement different logic according to the parameter input like:
.
private static void OnShowLocalTimeCommand(object sender, ExecutedRoutedEventArgs e)
{
var universalTime = DateTime.Now.ToUniversalTime();
string alertContent = String.Concat("Time in ", e.Parameter, ": ");

switch ((string)e.Parameter)
{
case "Sofia": { alertContent += universalTime.AddHours(2).ToShortTimeString(); break; }
case "Munich": { alertContent += universalTime.AddHours(1).ToShortTimeString(); break; }
case "Boston": { alertContent += universalTime.AddHours(-5).ToShortTimeString(); break; }
case "Dallas": { alertContent += universalTime.AddHours(-6).ToShortTimeString(); break; }
default: alertContent = "Unknown time zone"; break;
}

RadWindow.Alert(new DialogParameters() { Content = alertContent });            
}
 
See attached example demonstrating usage of RoutedUICommand.

Related Posts

Comments

Comments are disabled in preview mode.