C#:查询/计算过程中禁用按钮 [英] C#: Disable button during search/calculation
本文介绍了C#:查询/计算过程中禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我想要在搜索过程中禁用搜索按钮搜索对话框。这是当前的代码,但该按钮没有得到停用
查看:
<按钮内容=搜索
命令={结合StartSearchCommand}
IsEnabled ={结合IsNotSearching}/>
视图模型:
私人布尔_isNotSearching;
公共BOOL IsNotSearching
{
{返回_isNotSearching; }
组
{
_isNotSearching =价值;
OnPropertyChanged(IsNotSearching);
}
}
私人RelayCommand<对象> _startSearchCommand;
公众的ICommand StartSearchCommand
{
得到
{
如果(_startSearchCommand == NULL)
_startSearchCommand =新RelayCommand<对象>(P => ExecuteSearch ());
返回_startSearchCommand;
}
}
私人无效ExecuteSearch()
{
IsNotSearching = FALSE;
//做一些搜索在这里
IsNotSearching = TRUE;
}
解决方案
我做了一个 AsyncDelegateCommand
出于这个原因(基于著名 DelegateCommand
),它在执行命令操作期间内禁止命令(UI):
公共类AsyncDelegateCommand:ICommand的
{
只读动作<对象> _执行;
只读谓词<对象> _canExecute;
布尔_running;
公共事件的EventHandler CanExecuteChanged;
公共AsyncDelegateCommand(动作<对象>执行,谓语<对象> canExecute = NULL)
{
_execute =执行;
_canExecute = canExecute;
}
公共BOOL CanExecute(对象参数)
{
回报率(_canExecute == NULL真:?_canExecute(参数))及和放大器; !_running;
}
公共异步执行无效(对象参数)
{
_running = TRUE;
更新();
等待Task.Run(()=> _execute(参数));
_running = FALSE;
更新();
}
公共无效更新()
{
如果(CanExecuteChanged!= NULL)
CanExecuteChanged(这一点,EventArgs.Empty);
}
}
XAML:
<按钮命令={结合SomeCommand}... />
视图模型:
AsyncDelegateCommand SomeCommand {搞定; }
//在构造函数中
SomeCommand =新AsyncDelegateCommand(O => {Thread.sleep代码(5000);}); //代码运行
I have a search dialog where I want to disable the search button during the search. This is the current code but the button does not get deactivated
View:
<Button Content="Search"
Command="{Binding StartSearchCommand}"
IsEnabled="{Binding IsNotSearching}" />
ViewModel:
private bool _isNotSearching;
public bool IsNotSearching
{
get { return _isNotSearching; }
set
{
_isNotSearching = value;
OnPropertyChanged("IsNotSearching");
}
}
private RelayCommand<object> _startSearchCommand;
public ICommand StartSearchCommand
{
get
{
if (_startSearchCommand == null)
_startSearchCommand = new RelayCommand<object>(p => ExecuteSearch());
return _startSearchCommand;
}
}
private void ExecuteSearch()
{
IsNotSearching = false;
//do some searching here
IsNotSearching = true;
}
解决方案
I've made an AsyncDelegateCommand
for that reason (based on famous DelegateCommand
), it internally disable command (in UI) during executing command action:
public class AsyncDelegateCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
bool _running;
public event EventHandler CanExecuteChanged;
public AsyncDelegateCommand(Action<object> execute, Predicate<object> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return (_canExecute == null ? true : _canExecute(parameter)) && !_running;
}
public async void Execute(object parameter)
{
_running = true;
Update();
await Task.Run(() => _execute(parameter));
_running = false;
Update();
}
public void Update()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
xaml:
<Button Command="{Binding SomeCommand}" .../>
ViewModel:
AsyncDelegateCommand SomeCommand { get; }
// in constructor
SomeCommand = new AsyncDelegateCommand(o => { Thread.Sleep(5000); }); // code to run
这篇关于C#:查询/计算过程中禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文