CanExecute 和 CanExecuteChanged,我必须使用 RelayCommand 来实现这些吗?

发布于 2024-11-04 19:11:20 字数 2114 浏览 1 评论 0原文

我正在使用 MVVM-Light,并且我的中继命令运行良好,我刚刚读到我应该实现 CanExecuteChangedCanExecute。虽然我找不到一个好的例子。

有谁有一个关于如何实现这些的好例子。

CanExecute 在无法执行时需要返回 False,但不会只是禁用按钮?

我什么时候执行CanExecuteChanged

任何人都有关于何时使用每一个的好例子,我的代码可以在没有 此博客 帖子指出我应该实现这些项目。

我有点困惑,正如我所说,我想我只需将 Enabled 属性或其他内容绑定到 ViewModel 中的属性,以便我可以禁用按钮或类似的控件?

任何有助于理解的帮助将非常感激。

编辑

这就是我现在所拥有的...它可以工作,但按钮实际上并未禁用,只是命令没有运行,因为我返回了 false。我在构造函数中调用 CanExecuteMe 来强制 RaiseCanExecuteChanged 运行...

这在我的视图模型的构造函数中运行

        this.Page2Command = new RelayCommand(() => this.GoToPage2(), () => CanExecuteMe);

        CanExecuteMe = false;

,这是我的其余代码,我从一个示例中获取它。

    private bool _canIncrement = true;

    public bool CanExecuteMe
    {
        get
        {
            return _canIncrement;
        }

        set
        {
            if (_canIncrement == value)
            {
                return;
            }

            _canIncrement = value;

            // Update bindings, no broadcast
            //RaisePropertyChanged(CanIncrementPropertyName);

            Page2Command.RaiseCanExecuteChanged();
        }
    }

    public RelayCommand Page2Command
    {
        get;
        private set;
    }

    private object GoToPage2()
    {
        System.Windows.MessageBox.Show("Navigate to Page 2!");
        return null;
    }

这是我的 XAML

  <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="31,77,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Page2Command, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

I am using MVVM-Light and i have my relay command working perfectly, I have just read that i should be implementing CanExecuteChanged and CanExecute. Although i am unable to find a good example.

Does anyone have a good example of how to implement these.

CanExecute needs to return False when it can't be executed but wouldn't just disbale the button ??

When do i execute the CanExecuteChanged?

Anyone have any good examples of when to use each one, my code works without but this blog post states that I should be implementing these items.

I am a little confused, as I said I thought I would just bind the Enabled property or something to a property in the ViewModel so I can disable the button or a similar control?

Any help in understanding would be really grateful.

EDIT

This is what i have now... Its working but the button isn't physically DISABLED only the commmand doesn't run as i am returning false. I am calling CanExecuteMe in the constructor to force the RaiseCanExecuteChanged to run ...

This runs in my construtor of my viewmodel

        this.Page2Command = new RelayCommand(() => this.GoToPage2(), () => CanExecuteMe);

        CanExecuteMe = false;

and here is the rest of my code, i took it from an example.

    private bool _canIncrement = true;

    public bool CanExecuteMe
    {
        get
        {
            return _canIncrement;
        }

        set
        {
            if (_canIncrement == value)
            {
                return;
            }

            _canIncrement = value;

            // Update bindings, no broadcast
            //RaisePropertyChanged(CanIncrementPropertyName);

            Page2Command.RaiseCanExecuteChanged();
        }
    }

    public RelayCommand Page2Command
    {
        get;
        private set;
    }

    private object GoToPage2()
    {
        System.Windows.MessageBox.Show("Navigate to Page 2!");
        return null;
    }

And here is my XAML

  <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="31,77,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Page2Command, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-11-11 19:11:21

当 Button 需要确定是否应启用它时,会调用 CanExecute。

按钮在绑定时以及每次 CanExecuteChanged 触发后执行此操作(按钮监听其命令的此事件)。

因此,如果应禁用该按钮,您应该触发 CanExecuteChanged,并且当按钮调用 CanExecute 时,您应该返回 false。这是使用命令绑定时启用/禁用按钮的首选方法。

命令绑定使您能够将所有按钮逻辑封装在一个实例(命令)中。 CanExecute 方法应查询应用程序的当前状态,以确定是否应启用或禁用该按钮。通过这种封装,您可以减少视图模型中的意大利面条式代码,这些检查在这里、那里和那里执行,而我忘记了那里的那个。

CanExecute is called when the Button needs to determine if it should be enabled or not.

The Button does this on binding, and after every time CanExecuteChanged fires (the Button listens to this event for its Command).

So, if the button should be disabled, you should fire CanExecuteChanged and, when the button calls CanExecute, you should return false. This is the preferred method of enabling/disabling a button when using command bindings.

Command bindings enable you to encapsulate all the button logic within an instance (the Command). The CanExecute method should query the current state of the application to determine if the button should be enabled or disabled. By this encapsulation you reduce the spaghetti code in your View Model, where these checks are performed here and there and over there and I forgot about that one down there.

放赐 2024-11-11 19:11:21

使用 CanExecute 谓词时应该非常小心。它检查每个 UI 更改以及输入到任何字段的每个键盘按键。

这可能会导致性能问题!

You should be very careful using the CanExecute predicate. It checks on every UI change and for every keyboard key entered into ANY field.

This can cause performance issues!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文