简体   繁体   English

ICommand.CanExecute async

[英]ICommand.CanExecute async

I have a Command, which Check for CanExecute takes much of time. 我有一个Command,检查CanExecute需要很长时间。 Now, I ask me, if it is possible to run the Method for CanExecute asynchronous? 现在,我问我,是否可以运行CanExecute异步方法?

Well it is possible. 嗯有可能。 You should use the CanExecutedChanged . 您应该使用CanExecutedChanged Do your long lasting check in the background(you could use BackgroundWorker ), store the result if you can execute or not, fire the event and just return the cached value in your CanExecute . 在后台进行持久检查(可以使用BackgroundWorker ),如果可以执行或不执行,则存储结果,触发事件并返回CanExecute的缓存值。

No, you cannot directly run it asynchronously. 不,你不能直接异步运行它。 Nor should you, you do not know when the binding subsystem is going to call it. 您也不应该知道绑定子系统何时会调用它。

There is nothing stopping you from starting a background thread from within that function, but to be honest that would make very little sense. 没有什么可以阻止你从该函数中启动后台线程,但说实话,这没什么意义。 If your CanExecute code is taking that long to execute then you really need to re-evaluate what you are doing, whether that means redo the code, or redo the UI to remove the dependency on the CanExecute. 如果你的CanExecute代码需要很长时间才能执行,那么你真的需要重新评估你在做什么,这是否意味着重做代码,或重做UI以消除对CanExecute的依赖。

If you use the DelegateCommand<T> from Prism you can force anything bound to the command to re-evaluate the CanExecute when you choose. 如果您使用PrismDelegateCommand <T> ,您可以强制选择绑定到命令的任何内容来重新评估CanExecute。 This can be done by calling the RaiseCanExecuteChanged() function on the command. 这可以通过在命令上调用RaiseCanExecuteChanged()函数来完成。 If you then have a background thread running which calls this when necessary it should function in the manner you want. 如果你有一个后台线程运行,必要时调用它,它应该以你想要的方式运行。

CanExecute has to run on the GUI thread. CanExecute必须在GUI线程上运行。

But you can simply implement this for yourself: 但您可以自己实现:

Use a Boolean property that is updated from a Thread. 使用从Thread更新的Boolean属性。 When it is set, also call NotifyPropertyChanged (for the Command). 设置后,还要调用NotifyPropertyChanged(对于Command)。

It is probably best to default this property to false, but that depend on your domain logic. 最好将此属性默认为false,但这取决于您的域逻辑。

with ICommand implementations, checking CanExecute is purely optional , the only real benefit of using CanExecute is that some UI controls being bound to the command can listen to CanExecuteChanged event and Enable/Disable themselves. 使用ICommand实现,检查CanExecute是纯可选的 ,使用CanExecute的唯一真正好处是绑定到命令的某些UI控件可以监听CanExecuteChanged事件并启用/禁用它们自己。 There're many ways you can achieve this. 有很多方法可以实现这一目标。

You don't have to use CanExecute just for sake of following the conventions. 您不必仅为了遵守约定而使用CanExecute。 Why can't you start your long ruinning validation in a separate thread to avoid UI hangining and do the operation at the end of? 为什么你不能在一个单独的线程中开始你的长期破坏验证,以避免UI挂起并在最后执行操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RelayCommand ICommand.CanExecute不触发 - Relaycommand ICommand.CanExecute not firing 强制对ICommand.CanExecute进行重新评估 - Forcing Reevaluation on ICommand.CanExecute 在这种情况下如何调用ICommand.CanExecute? - How to call ICommand.CanExecute in this case? 在WPF中,如何将参数传递给ICommand.CanExecute() - In WPF, how to pass a parameter to ICommand.CanExecute() 即使设置了CommandParameter,ICommand.CanExecute也会传递null - ICommand.CanExecute being passed null even though CommandParameter is set MVVM ICommand.CanExecute参数包含先前的值 - MVVM ICommand.CanExecute parameter contains previous value 尽管ICommand.CanExecute计算为true,但按钮已禁用 - Button disabled although ICommand.CanExecute evaluates to true 为什么ButtonBase在测试`ICommand.CanExecute`之前不检查其可见性? - Why ButtonBase doesn't check its Visibility before testing `ICommand.CanExecute`? 当我将CommandParameter设置为某个Binding时,为什么我的ICommand.CanExecute(object)参数为null,但当我将其设置为某个静态值时为非null? - Why is my ICommand.CanExecute(object) parameter null when I set CommandParameter to some Binding, but non-null when I set it to some static value? 带有可选 canExecute 的异步 ICommand 实现 - Asyncronous ICommand implementation with optional canExecute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM