为什么即使我调用 CommandManager.InvalidateRequerySuggested() 也不会调用 WPF 按钮的命令的 CanExecute 方法? [英] Why WPF button's command's CanExecute method is not called even if I call CommandManager.InvalidateRequerySuggested()?

查看:19
本文介绍了为什么即使我调用 CommandManager.InvalidateRequerySuggested() 也不会调用 WPF 按钮的命令的 CanExecute 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这些问题中报告的相同问题:

I'm facing the same issue reported in these questions:

按钮命令 CanExecute 在属性更改时未调用

如何强制更改文本框以在 WPF 中启用我的命令?

(简而言之:我的命令链接按钮在它应该启用的时候没有被启用)但有一点不同:我已经尝试调用 CommandManager.InvalidateRequerySuggested(),没有结果.

(that briefly is: my command-linked button doesn't get enabled when it should) but with a slight difference: I already tried to invoke CommandManager.InvalidateRequerySuggested(), with no result.

最奇怪的是,只有当我触发"窗口上的任何事件(例如在窗口的任何点单击鼠标)时,按钮才会启用.

The weirdest thing is that the button gets enabled only as soon as I "trigger" any event on the window, like clicking with the mouse in any point of the window.

这是重现问题的代码:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.RunTask}" x:Name="cmdRunTask" CanExecute="CanExecuteRunTask" Executed="ExecuteRunTask">    </CommandBinding>
    <CommandBinding Command="{x:Static local:Commands.PushButton}" x:Name="cmdPushButton" CanExecute="CanPushButton" Executed="PushButton"></CommandBinding>
</Window.CommandBindings>
<StackPanel>
    <Button Content="Run task"
                x:Name="runButton"
                Command="{x:Static local:Commands.RunTask}"
                >
    </Button>
    <ProgressBar x:Name="progress"
                     Value="{Binding Path=Value, Mode=OneWay}"
                 Height="30"
                     >
    </ProgressBar>
    <Button Content="Press me if you dare"
                x:Name="pushButton"
                Command="{x:Static local:Commands.PushButton}"
                >
    </Button>
</StackPanel>

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new Model();
            ((Model)DataContext).PropertyChanged += DataContext_PropertyChanged;
            InitializeComponent();
        }

        private void DataContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Ready":
                    CommandManager.InvalidateRequerySuggested();
                    break;
            }
        }

        void CanExecuteRunTask(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void ExecuteRunTask(object sender, ExecutedRoutedEventArgs e)
        {
            ((Model)DataContext).RunLongTask();
        }

        void CanPushButton(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ((Model)DataContext).Ready;
        }

        void PushButton(object sender, ExecutedRoutedEventArgs e) { }
    }

    public static class Commands
    {
        public static readonly RoutedUICommand RunTask = new     RoutedUICommand();
        public static readonly RoutedUICommand PushButton = new     RoutedUICommand();
    }

    public class Model : INotifyPropertyChanged
    {
        private bool _ready = false;
        public bool Ready
        {
            get { return _ready; }
            set
            {
                _ready = value;
                RaisePropertyChanged("Ready");
            }
        }

        private int _value = 0;
        public int Value
        {
            get { return _value; }
            set
            {
                _value = value;
                RaisePropertyChanged("Value");
            }
        }

        public async void RunLongTask()
        {
            await RunLongTaskAsync();   
        }
        private Task RunLongTaskAsync()
        {
            this.Ready = false;
            Task t = new Task(() => {
                for (int i = 0; i <= 100; i++)
                {
                    Value = i;
                    System.Threading.Thread.Sleep(20);
                }
                this.Ready = true;
            });
            t.Start();
            return t;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

推荐答案

虽然PropertyChanged"事件会自动编组,但对 CommandManager.InvalidateRequerySuggested() 的调用不会.

While the "PropertyChanged" events are automatically marshalled, the call to CommandManager.InvalidateRequerySuggested() is not.

因此要解决此问题,只需确保在 Dispatcher 线程上进行调用,如下所示:

So to fix the issue just ensure the call is made on the Dispatcher thread as follows:

    private void DataContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "Ready":
                var d = Application.Current.Dispatcher;
                if (d.CheckAccess())
                    CommandManager.InvalidateRequerySuggested();
                else
                    d.BeginInvoke((Action)(() => { CommandManager.InvalidateRequerySuggested(); }));
                break;
        }
    }

在依赖异步任务触发的属性更改时要小心.

Be careful when relying on property changes that are triggered by asynchronous tasks.

我希望这可以帮助你们中的一些人节省一些工作时间...

I hope this can help some of you in saving some days of work...

这篇关于为什么即使我调用 CommandManager.InvalidateRequerySuggested() 也不会调用 WPF 按钮的命令的 CanExecute 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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