基于@ stuart的blog post,我可以看到如何将文字字符串绑定为CommandParameter。
local:MvxBind="Click FindUserCommand, CommandParameter=foo"
public class FindUserViewModel: ViewModelBase
{
public FindUserViewModel(IFindUserCommand findUserCommand)
{
_findUserCommand = findUserCommand;
}
// class truncated for brevity
private IFindUserCommand _findUserCommand;
public IFindUserCommand FindUserCommand
{
get { return _findUserCommand; }
set { _findUserCommand = value; RaisePropertyChanged(() = > FindUserCommand); }
}
}
public class FindUserCommand: IFindUserCommand
{
// class truncated for brevity
public void Execute(object parameter)
{
// RIGHT HERE:
// value of parameter == foo
}
}
当命令存在于自己的命令对象而不是ViewModel中时,如何将整个ViewModel绑定为Button的CommandParame?
public void Execute(object parameter)
{
var vm = (FindUserViewModel)parameter;
var firstName = vm.FirstName;
var lastName = vm.LastName;
// etc...
}
注意:如果我必须以某种方式扩展MvvmCross以类似于WPF的统一方式制作此功能,我就可以了......我只是不知道从哪里开始。 的
我找到的所有示例都直接在ViewModel中声明了Command,如下所示:
MvvmCross示例
private Cirrious.MvvmCross.ViewModels.MvxCommand _insertCommand;
public System.Windows.Input.ICommand InsertCommand
{
get
{
_insertCommand = _insertCommand ? ? new Cirrious.MvvmCross.ViewModels.MvxCommand(DoInsert);
return _insertCommand;
}
}
private int _countAdded = 0;
private void DoInsert()
{
_countAdded++;
var kitten = _kittenGenesisService.CreateNewKitten(_countAdded.ToString());
_dataService.Insert(kitten);
RefreshDataCount();
}
然后将按钮绑定到View,如下所示:
<Button
android:text="Insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
local:MvxBind="Click InsertCommand" />
但我想在自己的类文件中声明Command对象,以便它更可测试,然后将其注入ViewModel。
在MVVMCross for Android中是否可以绑定CommandParameter,类似于WPF如何进行命令参数绑定?
WPF示例:
查看按钮定义:
<Button
Command="{Binding SearchCommand}"
CommandParameter="{Binding}"/>
ViewModel命令属性:
private ICommand _findUserCommand;
public ICommand FindUserCommand
{
get { return _findUserCommand; }
set { _findUserCommand = value; RaisePropertyChanged( () => FindUserCommand ); }
}
命令对象定义:
public class FindUserCommand : ICommand
{
private readonly IUserService _userService;
public FindUserCommand(IUserService userService)
{
_userService = userService;
}
public bool CanExecute ( object parameter )
{
var findUserViewModel = ( FindUserViewModel )parameter;
return findUserViewModel.NameSearch.Length > 0;
}
public void Execute ( object parameter )
{
FindUserViewModel.Users = _userService.Find( FindUserViewModel.NameSearch );
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged ()
{
if ( CanExecuteChanged != null )
CanExecuteChanged( this, new EventArgs() );
}
}
答案 0 :(得分:2)
Android小部件(如按钮)默认情况下没有Command和CommandParameter成员 - 因此无法使用它们&#34;开箱即用&#34;。
但是,您可以轻松地从Button
继承以生成MyButton
,然后您可以根据需要提供Command
和CommandParameter
成员,例如类似的东西:
public class MyButton : Button
{
public MyButton(Context c, IAttributeSet a) : base(c,a)
{
Click += (s,e) => {
if (Command == null) return;
if (!Command.CanExecute(CommandParameter)) return;
Command.Execute(CommandParameter);
};
}
public ICommand Command { get;set; }
public object CommandParameter { get;set; }
}
使用此功能,您可以在axml中使用MyButton
代替Button
<MyButton
local:MvxBind="Command SearchCommand;CommandParameter ." />
有关数据绑定的更多信息,请参阅https://github.com/MvvmCross/MvvmCross/wiki/Databinding
或者,另一种方法是使用基于ValueConverter的方法 - 类似于MvvmCross提供的CommandParameter功能 - 请参阅http://slodge.blogspot.co.uk/2013/06/commandparameter-binding.html