Understanding ICommand implementation without MVVM

泪湿孤枕 提交于 2019-12-24 03:17:10

问题


I am trying to understand how to work with Commands. I read a lot about commands, and I know, that most times commands are used within the MVVM pattern. Also I know, that there is a RoutedCommand class - which is often used to save time while developing.

But - I want to understand the basics - and exactly that's the problem. Here we go:

In my app I defined a class 'MyCommand' :

 public class MyCommand :ICommand
{
    public void Execute(object parameter)
    {
        Console.WriteLine("Execute called!");
        CanExecuteChanged(null, null);
    }

    public bool CanExecute(object parameter)
    {
        Console.WriteLine("CanExecute called!");
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

Well - to get a static access I decided to make a class, just for all application commands:

 public static class AppCommands
    {
        private static ICommand anyCommand = new MyCommand();

        public static ICommand AnyCommand
        {
            get { return anyCommand; }
        }
    }

Nearly there. Now I put two buttons in my MainWindow. One of them is 'binded' to the command:

<StackPanel>
    <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
    <Button Content="Button" Height="23" Name="button2" Width="75" Command="{x:Static local:AppCommands.AnyCommand}" CommandParameter="Hello"/>
</StackPanel>

And here is the MainWindow.cs :

public MainWindow()


  {
        InitializeComponent();
        AppCommands.AnyCommand.CanExecuteChanged += MyEventHandler;
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
       // Nothing for the moment
    }

    private void MyEventHandler(object sender, EventArgs e)
    {
        Console.WriteLine("MyEventHandler called");
    }

So - let's run my project. As you can see, I made some console outputs. If I click on button2, the output is:

CanExecute called! Execute called! CanExecute called! MyEventHandler called

So, in my eyes this is what happens: 1.) the command on the button is 'activated'. To check if the execute method should be called, the CanExecute method is called. 2.) If the CanExecute method returns true, then the Execute method is called. 3.) Within the execute method, I defined, that the event 'CanExecuteChanged' should be raised. Calling it will 1st check 'CanExecute' and then call the eventhandler.

This is not clear to me. The only way to call the event is within the Execute method. But the Execute method is called via Command logic after checking CanExecute. Calling the event also checks CanExecute, but why? I am confused.

And things are getting more confused, when I try to disable the button. Lets say, there is a 'CommandParameter' - and 'CanExecute' now works with it. So it could be, that the method returns false. In this case the button is disabled - okay.

But: how do I reactivate it? As we already know: I can only raise the CanExecuteChange event from within my command class. So - as we are not able to click the disabled button - the command won't call CanExecute (or even Execute) method.

Seems to me there is something important I can't see - but I really can't find it.

Could you please help me?


回答1:


Your CanExecuteChanged shouldn't be raised by Execute, it should be raised when CanExecute will start returning a different value. When that is depends on your command class. In the simplest form, you could add a property:

public class MyCommand : ICommand
{
    bool canExecute;

    public void Execute(object parameter)
    {
        Console.WriteLine("Execute called!");
    }

    public bool CanExecute(object parameter)
    {
        Console.WriteLine("CanExecute called!");
        return CanExecuteResult;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecuteResult
    {
        get { return canExecute; }
        set {
            if (canExecute != value)
            {
                canExecute = value;
                var canExecuteChanged = CanExecuteChanged;
                if (canExecuteChanged != null)
                    canExecuteChanged.Invoke(this, EventArgs.Empty);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/10812323/understanding-icommand-implementation-without-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!