当CanExecute叫什么名字?叫什么名字、CanExecute

2023-09-02 01:37:13 作者:等你浪够逼也烂了

在演示中,我有一个按钮来切换一个布尔字段 isAsking 。我创建一个可以执行的命令,只有当 isAsking ==真

在我preSS切换按钮, okButton.IsEnable 立即更改,这表明该命令将查找 isAsking 。

我觉得很困惑,为什么命令对象注意到一个领域,当 CanExecute 将被调用?

的变化

书面方式虽然有一段时间WPF应用程序,我是新来的WPF命令。请这种情况给出一个交代,如果可能的话,指出一些相关的文章或博客(我已经读过太多的文章在谈论剪切/粘贴命令)。

 <窗​​口的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        X:类=WpfApplication1.MainWindow
        标题=主窗口高度=350宽度=525X:名称=的主窗口>
    < StackPanel的>
        <按钮名称=okButtonCONTENT =确定/>
        <按钮内容=切换点击=Button_Click_1/>
    < / StackPanel的>
< /窗>
 
C语言出现错误Command line error D2027 cannot execute ac1a Error executing cl.exe.见图片

code-背后:

 公共部分类主窗口:窗口
{
    私人布尔isAsking;

    公共主窗口()
    {
        的InitializeComponent();

        CB的CommandBinding =新的CommandBinding();
        cb.Command = okCommand;
        cb.CanExecute + = CanOKExecute;
        cb.Executed + = cb_Executed;
        mainWindow.CommandBindings.Add(CB);
        okButton.Command = okCommand;
    }

    私人的RoutedCommand okCommand =新的RoutedCommand(OK的typeof(主窗口));


    无效cb_Executed(对象发件人,ExecutedRoutedEventArgs E)
    {

    }

    无效CanOKExecute(对象发件人,CanExecuteRoutedEventArgs E)
    {
        e.CanExecute = isAsking;
    }

    私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
    {
        isAsking = isAsking!;
    }
}
 

解决方案

我试着搜索的命令管理检测条件,达到的这个方位的文章。

通过检查.NET Framework的源$ C ​​$ C,笔者认为,命令管理不检测条件本身,而不是当 Keyboard.KeyUpEvent Mouse.MouseUpEvent Keyboard.GotKeyboardFocusEvent Keyboard.LostKeyboardFocusEvent 时,它会重新评估CanExecute方法。

本文包含的其他信息,但上面的部分已经足够了。

In the demo, I have a button to toggle a bool field isAsking. I create a command which can execute only when isAsking==true.

Once I press Toggle button, okButton.IsEnable changes immediately, which indicates the command finds the change of isAsking.

I feel very confused why the command object notices the change of a field, when CanExecute will be called?

Although writting WPF application for some time, I'm new to WPF Command. Please give an explaination to this case and if possible, point out some related articles or blogs (I've already read too many articles talking about cut/paste command).

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525" x:Name="mainWindow" >
    <StackPanel>
        <Button Name="okButton" Content="Ok" />
        <Button Content="Toggle"  Click="Button_Click_1"/>
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    private bool isAsking;

    public MainWindow()
    {
        InitializeComponent();

        CommandBinding cb = new CommandBinding();
        cb.Command = okCommand;
        cb.CanExecute += CanOKExecute;
        cb.Executed += cb_Executed;
        mainWindow.CommandBindings.Add(cb);
        okButton.Command = okCommand;
    }

    private RoutedCommand okCommand = new RoutedCommand("ok", typeof(MainWindow));


    void cb_Executed(object sender, ExecutedRoutedEventArgs e)
    {

    }

    void CanOKExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = isAsking;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        isAsking = !isAsking;
    }
}

解决方案

I try to search for "the CommandManager detects conditions" and reach this exellent article.

By examining .NET Framework source code, the author finds that the CommandManager doesn't detect conditions by itself, rather than when Keyboard.KeyUpEvent, Mouse.MouseUpEvent, Keyboard.GotKeyboardFocusEvent, or Keyboard.LostKeyboardFocusEvent occurs, it will reevaluate CanExecute method.

The article includes other information, but the above part has been enough for me.