如何更新命令';s";CanExecute";来自不同线程的值
本文关键字:quot CanExecute 线程 更新 命令 何更新 | 更新日期: 2023-09-27 18:00:57
在我的应用程序中,我有一个命令,我只希望用户在尚未运行时能够触发它。有问题的命令绑定到一个WPF按钮,这意味着如果CanExecute为false,它会自动禁用该按钮。到目前为止还不错。
不幸的是,该命令执行的操作运行时间很长,因此需要在不同的线程上执行。我不认为这会是个问题。。。但看起来确实如此。
我提取了一个最小的样本来说明这个问题。如果绑定到按钮(通过LocalCommands.Problem静态引用(,该按钮将根据需要被禁用。当工作线程尝试更新CanExecute时,将从System.Windows.Controls.Primitives.ButtonBase.内部引发InvalidOperationException
解决这个问题最合适的方法是什么?
下面的示例命令代码:
using System;
using System.Threading;
using System.Windows.Input;
namespace InvalidOperationDemo
{
static class LocalCommands
{
public static ProblemCommand Problem = new ProblemCommand();
}
class ProblemCommand : ICommand
{
private bool currentlyRunning = false;
private AutoResetEvent synchronize = new AutoResetEvent(false);
public bool CanExecute(object parameter)
{
return !CurrentlyRunning;
}
public void Execute(object parameter)
{
CurrentlyRunning = true;
ThreadPool.QueueUserWorkItem(ShowProblem);
}
private void ShowProblem(object state)
{
// Do some work here. When we're done, set CurrentlyRunning back to false.
// To simulate the problem, wait on the never-set synchronization object.
synchronize.WaitOne(500);
CurrentlyRunning = false;
}
public bool CurrentlyRunning
{
get { return currentlyRunning; }
private set
{
if (currentlyRunning == value) return;
currentlyRunning = value;
var onCanExecuteChanged = CanExecuteChanged;
if (onCanExecuteChanged != null)
{
try
{
onCanExecuteChanged(this, EventArgs.Empty);
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.Message, "Exception in event handling.");
}
}
}
}
public event EventHandler CanExecuteChanged;
}
}
更改:
onCanExecuteChanged(this, EventArgs.Empty);
至:
Application.Current.Dispatcher.BeginInvoke((Action)(onCanExecuteChanged(this, EventArgs.Empty)));
编辑:
原因是WPF正在侦听这些事件,并试图在UI元素中执行操作(即在Button
中切换IsEnabled
(,因此必须在UI线程中引发这些事件。