了解ICommand的实现,而不MVVM [英] Understanding ICommand implementation without MVVM

查看:217
本文介绍了了解ICommand的实现,而不MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我想了解如何与命令的工作。
我红了很多有关命令,我知道,那最次的命令MVVM模式中使用。我也知道,有一个的RoutedCommand类 - 它经常被用来保存在开发时间

today I am trying to understand how to work with Commands. I red 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:

在我的应用我定义一个类'MyCommand':

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>

和这里是MainWindow.cs:

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");
    }



所以 - 我们来运行我的项目。正如你所看到的,我做了一些控制台输出。
。如果我点击按钮2,输出为:

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

CanExecute呼吁!
,可执行叫!
CanExecute呼吁!
一个MyEventHandler名为

CanExecute called! Execute called! CanExecute called! MyEventHandler called

所以,在我眼里,这是发生了什么:
1)被激活按钮上的命令。要检查是否执行方法应该被调用时,CanExecute方法被调用。
2)如果CanExecute方法返回true,则调用Execute方法。
3)在execute方法中,我定义,该事件CanExecuteChanged'应该提高。调用它会检查1号CanExecute,然后调用事件处理程序。

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.

这是我不清楚。调用该事件的唯一方法是Execute方法中。但是,Execute方法是检查CanExecute后通过命令逻辑调用。调用事件还检查CanExecute,但为什么呢?我很困惑。

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.

和事情变得更加混乱,当我尝试禁用按钮。
比方说,有一个CommandParameter - 和CanExecute现在与它。所以它可能是,该方法返回false。在这种情况下该按钮被禁用 - 好吧

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.

不过:我怎么激活呢?正如我们已经知道:我只能从我的命令类中提高CanExecuteChange事件。所以 - 因为我们无法单击禁用的按钮 - 该命令将不会调用CanExecute(甚至执行)的方法。

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 :)

能否请你帮我吗?
非常感谢
CodeCannibal

Could you please help me? Thanks a lot CodeCannibal

推荐答案

CanExecuteChanged 不应执行,就应提高,当 CanExecute 将开始返回不同的值。当那是取决于你的命令类。最简单的形式,你可以添加一个属性:

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);
            }
        }
    }
}

这篇关于了解ICommand的实现,而不MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆