如何在 Mvvmcross 中使用 CanExecute [英] How to use CanExecute with Mvvmcross
问题描述
我有一个按钮
<Button
android:id="@+id/ButtonConnect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disconnect"
local:MvxBind="{'Click':{'Path':'DisconnectCommand'}}" />
我有一个命令
public IMvxCommand DisconnectCommand
{
get
{
return new MvxRelayCommand(this.GetService<IConnectionService>().Disconnect);
}
}
然后我想启用/禁用 DisconnectCommand 使用
Then I want to enable/disable the DisconnectCommand using
DisconnectCommand.CanExecute(this.GetService<IConnectionService>().UsbConnected);
但这显然是错误的(它不起作用),我将检查作为参数输入,但通常我会这样做
But thats clearly wrong (It is not working), I put the check in as a a parameter, but normally I would do
DisconnectCommand.CanExecute = someBool;
但是没有要设置的属性,那么如何处理呢?
But there is no property to set, so how to go about this?
推荐答案
要了解如何使用 CanExecute
,请查看 Silverlight 或 WPF - 有很多博客讨论如何使用使用 ICommand
- 例如http://weblogs.asp.net/nmarun/archive/2009/12/02/using-icommand-silverlight-4.aspx 或 http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx
To work out how to use CanExecute
, take a look at Silverlight or WPF - there's lots of blogs out there which talk about how to use ICommand
- e.g. http://weblogs.asp.net/nmarun/archive/2009/12/02/using-icommand-silverlight-4.aspx or http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx
一个例子是:
private MvxRelayCommand _disconnectCommand;
public IMvxCommand DisconnectCommand
{
get
{
if (_disconnectCommand == null)
_disconnectCommand = new MvxRelayCommand(this.GetService<IConnectionService>().Disconnect, item => this.IsItemConnected(item));
return _disconnectCommand;
}
}
private void SomeServiceNotificationHandler()
{
_disconnectCommand.RaisePropertyChanged();
}
private bool IsItemConnected(object thing)
{
return /* your code */;
}
<小时>
不过有一个小问题......
There is one small problem though....
CanExecute
并没有真正在所有平台上的所有 MvxBindings 中完全实现......它适用于其中一些,但对于其中一些它不会 - 而我没有真的知道目前有哪些!如果您遇到问题,请告诉我(通过 GitHub 问题),他们会得到解决...
CanExecute
isn't really fully implemented across all the MvxBindings across all platforms... It will work for some of them, but for some of them it won't - and I don't really know which ones at present! If you come across issues, then please let me know (via GitHub issues) and they will get fixed...
就个人而言...我不倾向于使用 CanExecute
- 我倾向于使用单独的布尔属性,然后我将其绑定到控件上可用的任何属性 - 例如大多数控件都有类似 Enabled
、IsEnabled
、Disabled
、IsDisabled
等.
Personally... I don't tend to use CanExecute
- I tend instead to use a separate Boolean property which I then bind to whatever property is available on the control - e.g. most controls have something like Enabled
, IsEnabled
, Disabled
, IsDisabled
, etc.
我通常发现设置布尔属性比调用RaiseCanExecuteChanged
I generally find it easier (and more readable) to set the Boolean property rather than to call RaiseCanExecuteChanged
例如我会使用类似的东西:
e.g. I'd use something like:
<Button
android:id="@+id/ButtonConnect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disconnect"
local:MvxBind="{'Click':{'Path':'DisconnectCommand'},'Enabled':{'Path':'UsbConnected'}}" />
您可以肯定地认为 CanExecute
方法具有优势 - 因为它将命令逻辑全部保存在一个对象中,并且因为它可用于防止发生 Execute
调用在 RelayCommand
中.这就是为什么我很乐意尝试修复 mvvmcross 绑定中的 CanExecute
错误.
You can definitely argue that the CanExecute
approach has advantages - because it keeps the Command logic all in one object, and because it can be used to prevent Execute
calls happening within the RelayCommand
. That's why I'm happy to try to fix CanExecute
bugs in mvvmcross bindings as we find them.
这篇关于如何在 Mvvmcross 中使用 CanExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!