RoutedUICommand: Where's Execute()?

Aug 10, 2009 at 5:08 PM

I've successfully specified the Commands namespace in the xaml header, and specified the Command and CommandParameter for each button in the xaml. I've created the MyCmd_Executed(object sender, ExecutedRoutedEventArgs e) and MyCmd_CanExecute(object sender,CanExecuteRoutedEventArgs e) in the C# code and hit those when expected.

My question: I need an Execute method for my command logic, but cannot find that in either ICommand or RoutedUICommand. I need something that looks like the following:

public returnStatus Execute(object parameter)

Am I really suppose to put my command logic in MyCmd_Executed(), even though the name imples that this method gets called after the command executes (some examples do this)? That strikes me as pretty bad.

Thanks,

Randy

Aug 10, 2009 at 5:54 PM

Somehow, I must have been getting ICommand from someplace other than Syste.Windows.Input. I created a class for a specific command:

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

public

</font></font><font size="2" color="#0000ff">

 

</font>

class AbortForecastCmd : ICommand

In myCommands class, I create the command in the constructor. This starts where many of the online examples start for RoutedUICommand:

<font size="2">

_abortForecast =

</font>

new AbortForecastCmd("Abort Forecast Download","AbortForecast", typeof(EdswCommands));

I call the following in myCommands from the constructor in the code-behind class:

pubic static void BindCommandsToWindow(UserControl userControl)
        {
            userControl.CommandBindings.Add(
                new CommandBinding(AbortForecast, AbortForecast_Executed, AbortForecast_CanExecute));
}

When I click the button in xaml, the Execute in AbortForecastCmd get executed. Yeah!

Randy

Aug 10, 2009 at 6:24 PM

Since my last post looked terrible, I'll try again:

Class that contains my specific command:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace BackEnd
{
    public class AbortForecastCmd : ICommand
    {
        private string _text = string.Empty;
        private string _name = string.Empty;
        private Type _ownerType;
        public AbortForecastCmd(string text, string name, Type ownerType)
        {
            _text = text;
            _name = name;
            _ownerType = ownerType;
        }

        #region Implementation of ICommand

        public void Execute(object parameter)
        {
            System.Windows.MessageBox.Show("Execute Works for AbortForecastCmd");
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        #endregion
    }
}

Class that processes commands:

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace BackEnd
{
    public static class EdswCommands
    {
        #region Private Command Variables

        private static AbortForecastCmd _abortForecast;

        #endregion Private Command Variables

        static EdswCommands()
        {
            _abortForecast = new AbortForecastCmd("Abort Forecast Download",
                            "AbortForecast", typeof(EdswCommands));
        }

        #region Public Command Properties

        // Command: AddContact
        public static AbortForecastCmd AbortForecast
        {
            get { return _abortForecast; }
        }


        #endregion Public Command Properties


        #region Public Command Event Handlers


        public static void AbortForecast_Executed(object sender,
                   ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("AbortForecast command executed");
        }
        public static void AbortForecast_CanExecute(object sender,
                           CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }


        #endregion Public Command Event Handlers


        public static void BindCommandsToWindow(UserControl userControl)
        {
            userControl.CommandBindings.Add(
                new CommandBinding(AbortForecast, AbortForecast_Executed, AbortForecast_CanExecute));

        }

    }
}

From my code-behind class:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace UserInterface
{
    public class CommandParameter
    {
        public bool CanEditBeExecuted { get; set; }
    }
    /// 
    /// Interaction logic for WeatherConfigurationControl.xaml
    /// 
    public partial class WeatherConfigurationControl : UserControl
    {
        #region Private Members
        EdswWeatherConfigurationInfo _wci;
        public CommandParameter parameter { get; set; }

        #endregion


        public WeatherConfigurationControl()
        {
            //If we don't do this before InitializeComponent(), the buttons
            //don't have anything to bind to.
            EdswCommands.BindCommandsToWindow(this);

            InitializeComponent();

            //Setup binding through button event handlers
            _wci = new EdswWeatherConfigurationInfo();

            //Connects all single-item controls like 
            //text boxes to data source in BackEnd.
            this.DataContext = _wci;

            //Setup binding through xaml
            parameter = new CommandParameter();
            parameter.CanEditBeExecuted = true;

            Loaded += new RoutedEventHandler(WeatherConfigurationControl_Loaded);
        }

        void WeatherConfigurationControl_Loaded(object sender, RoutedEventArgs e)
        {
            EdswCommands.BindCommandsToWindow(this);
        }

    }
}

xaml code header and button entry:

xmlns:Commands="clr-namespace:BackEnd;assembly=BackEnd"


<button>
</button>