C# WPF在命令运行时禁用该命令一段时间
我需要在按钮运行时禁用它。我有以下代码: RelayCommand.cs这是我的命令类C# WPF在命令运行时禁用该命令一段时间,c#,wpf,xaml,async-await,C#,Wpf,Xaml,Async Await,我需要在按钮运行时禁用它。我有以下代码: RelayCommand.cs这是我的命令类 class RelayCommand : ICommand { readonly Action<object> _execute; readonly Predicate<object> _canExecute; public RelayCommand(Action<object> execute) : this(execute, n
class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
MainWindow.xaml这是绑定命令的xaml代码
<Button x:Name="getTextButton" Command="{Binding GetTextCommand}" CommandParameter="{Binding ElementName=textTypeSelector, Path=SelectedIndex}"/>
问题是您正在为CanExecute委托传递true。向它传递一个每次需要求值时都将执行的方法,在该方法中,您可以测试该命令是否应该是可执行的。向ViewModel添加一个布尔值,以指示该命令正在执行,并在GetText()方法中设置布尔值 然后将您的RelayCommand更改为以下命令
_getTextCommand = new RelayCommand(this.GetText,CanGetText);
尝试以下操作:在视图模型中添加布尔属性,并在视图模型中实现
INotifyPropertyChanged
private bool isEnable = true;
public bool IsEnable
{
get { return isEnable; }
set
{
isEnable = value;
NotifyPropertyChanged();
}
}
public async void GetText(object o)
{
await Task.Factory.StartNew(() =>
{
IsEnable = false;
});
IsEnable = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
将其绑定到button IsEnable属性
<Button x:Name="abc"
Command="{Binding GetTextCommand}"
IsEnabled="{Binding IsEnable}" />
将IsEnable设置为您想要的任何位置。这里是我正在使用的一个实现。它不需要ViewModel中的附加属性
public sealed class AsyncDelegateCommand : ICommand
{
private readonly Func<bool> _canExecute;
private readonly Func<Task> _executeAsync;
private Task _currentExecution;
public AsyncDelegateCommand(Func<Task> executeAsync)
: this(executeAsync, null)
{
}
public AsyncDelegateCommand(Func<Task> executeAsync, Func<bool> canExecute)
{
if (executeAsync == null) throw new ArgumentNullException("executeAsync");
_executeAsync = executeAsync;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (_currentExecution != null && !_currentExecution.IsCompleted)
{
return false;
}
return _canExecute == null || _canExecute();
}
public async void Execute(object parameter)
{
try
{
_currentExecution = _executeAsync();
await _currentExecution;
}
finally
{
_currentExecution = null;
CommandManager.InvalidateRequerySuggested();
}
}
}
公共密封类AsyncDelegateCommand:ICommand
{
私有只读功能可执行;
私有只读函数executeAsync;
私有任务_currentExecution;
公共AsyncDelegateCommand(Func executeAsync)
:此(executeAsync,null)
{
}
公共AsyncDelegateCommand(Func executeAsync,Func canExecute)
{
如果(executeAsync==null)抛出新的ArgumentNullException(“executeAsync”);
_executeAsync=executeAsync;
_canExecute=canExecute;
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
公共布尔CanExecute(对象参数)
{
if(_currentExecution!=null&!_currentExecution.IsCompleted)
{
返回false;
}
return _canExecute==null | | | u canExecute();
}
公共异步void Execute(对象参数)
{
尝试
{
_currentExecution=_executeAsync();
等待执行;
}
最后
{
_currentExecution=null;
CommandManager.InvalidateRequestSuggested();
}
}
}
不错,但有一个问题。代码完成后,按钮不会设置为启用状态。单击后,它将设置为启用状态。如何解决?@user2932802好的,我认为您需要在ViewModel中使用CanExecute方法。我将更新代码。通常情况下,CommandManager.InvalidateRequestSuggested()应检查并启用按钮状态。您可以使用new RelayCommand(this.GetText,CanGetText)缩短按钮状态代码>更新。若我最小化窗口或在随机位置单击它,按钮将被启用。我想做的事情是将RX与ICommand一起使用。我已经被可靠地告知,它有助于消除您看到的按钮被禁用或基于CanExecute的一些问题。看看这里和这里
private bool isEnable = true;
public bool IsEnable
{
get { return isEnable; }
set
{
isEnable = value;
NotifyPropertyChanged();
}
}
public async void GetText(object o)
{
await Task.Factory.StartNew(() =>
{
IsEnable = false;
});
IsEnable = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
<Button x:Name="abc"
Command="{Binding GetTextCommand}"
IsEnabled="{Binding IsEnable}" />
public sealed class AsyncDelegateCommand : ICommand
{
private readonly Func<bool> _canExecute;
private readonly Func<Task> _executeAsync;
private Task _currentExecution;
public AsyncDelegateCommand(Func<Task> executeAsync)
: this(executeAsync, null)
{
}
public AsyncDelegateCommand(Func<Task> executeAsync, Func<bool> canExecute)
{
if (executeAsync == null) throw new ArgumentNullException("executeAsync");
_executeAsync = executeAsync;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (_currentExecution != null && !_currentExecution.IsCompleted)
{
return false;
}
return _canExecute == null || _canExecute();
}
public async void Execute(object parameter)
{
try
{
_currentExecution = _executeAsync();
await _currentExecution;
}
finally
{
_currentExecution = null;
CommandManager.InvalidateRequerySuggested();
}
}
}