Xamarin中传递的RelayCommand参数 [英] RelayCommand parameter passing in Xamarin
问题描述
我对Xamarin cross-platform
非常陌生,尽管我确实对WPF
和MVVM
有一定的经验,但是在理解使用以下ICommand
实现的参数化RelayCommand
调用时仍然遇到问题.有人可以解释如何正确地将视图中的CommandParameter
从我的视图传递到绑定的RelayCommand
中,因为这似乎与RelayCommand
的普通WPF
版本不同:
I am very new to Xamarin cross-platform
and while I did have some experience with WPF
and MVVM
I am still having issue understanding parameterized RelayCommand
invocation using ICommand
implementation below. Can someone explain how to properly pass and receive a CommandParameter
from my View into my bound RelayCommand
as this seems quiet different from a normal WPF
version of RelayCommand
:
/// <summary>
/// A command whose sole purpose is to relay its functionality
/// to other objects by invoking delegates.
/// The default return value for the CanExecute method is 'true'.
/// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
/// <see cref="CanExecute"/> is expected to return a different value.
/// </summary>
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
/// <summary>
/// Raised when RaiseCanExecuteChanged is called.
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
/// <returns>true if this command can be executed; otherwise, false.</returns>
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}
/// <summary>
/// Executes the <see cref="RelayCommand"/> on the current command target.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
public void Execute(object parameter)
{
_execute();
}
/// <summary>
/// Method used to raise the <see cref="CanExecuteChanged"/> event
/// to indicate that the return value of the <see cref="CanExecute"/>
/// method has changed.
/// </summary>
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
在WPF之前,我曾经有过类似的东西:
Before in WPF I used to have something like:
<Command="{Binding OpenMenuItemCommand}"
CommandParameter="{Binding SelectedItem}"/>
以及ViewModel
一侧:
OpenMenuItemCommand = new RelayCommand(OpenMenuItem);
...
public void OpenMenuItem(object sender, ItemTappedEventArgs args)
{
}
所以我的参数将通过args
来输入.
So my parameter would come through args
.
推荐答案
我相信您将事件和命令弄糊涂了.两者之间的某些区别是您需要订阅事件,并且必须发生事件.任何人都可以调用命令,也可以将其阻止.
I believe you are getting events and commands confused. Some of the difference between the two are that you need to subscribe to events and events must occur. Commands can be called by anyone and also have the ability to be blocked.
因此,为了使您的示例正常工作,您应该修改代码以允许RelayCommand对参数进行操作.此参数将定义参数的类型.我会使用类似 MVVMLight 之类的内容,其中包含
So to get you example to work correctly you should modify your code to allow your RelayCommand to take an action with a parameter. This parameter will define the Type of the parameter. I would use something like MVVMLight which contains a Generic RelayCommand so that you don't have to write your own. Once that is done you should be able to change your code to look like this.
OpenMenuItemCommand = new RelayCommand<MenuItem>(OpenMenuItem);
...
public void OpenMenuItem(MenuItem item)
{
}
我写了一个小的博客文章,其中包含一个完整的工作项目,如果您想查看一个有效的示例.
I wrote a small blog post that contains a full working project if you want to see a working example.
这篇关于Xamarin中传递的RelayCommand参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!