如何使用CanExecute与Mvvmcross [英] 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
eg我会使用以下东西:
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
具有优点 - 因为它将Command逻辑全部保存在一个对象中,并且因为它可以用于阻止 RelayCommand $中执行
执行
调用c $ c>。这就是为什么我很高兴在我们找到它们时,尝试修复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.
这篇关于如何使用CanExecute与Mvvmcross的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!