所以,我有一个元素,它有一个带2个参数的命令。
我之前使用我发现的代码片段做了这件事,但是在我的生活中不能记住如何做或者再次找到它。
所以,这是我之前创建的多值转换器:
public class MultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
return values.Clone();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return (value as string).Split(' ');
}
}
现在,我需要在ICommand中分配我想要调用的函数。我通常使用类似于:
的行enemyPopupTooltip = new RelayCommand(param => this.EnemyPopupTooltipEx(param),null);
然而,当它的多值时,这不会起作用。如何使用我的relay命令将2个参数(使用多值转换器)传递到我的函数中?
作为参考,这里是relaycommand类中的所有内容:
public class RelayCommand : ICommand
{
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class.
/// </summary>
/// <param name="execute">The execute.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class.
/// </summary>
/// <param name="execute">The execute.</param>
/// <param name="canExecute">The can execute.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Defines the method that determines whether the command 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(parameter);
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </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(parameter);
}
/// <summary>
/// Action
/// </summary>
private readonly Action<object> _execute;
/// <summary>
/// Predicate
/// </summary>
private readonly Predicate<object> _canExecute;
答案 0 :(得分:2)
然而,当它的多值
时,这不会起作用
这个假设是错误的。它确实有效!
当您的多转换器返回值数组时,此数组将作为参数传递给Command.Execute方法。
- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
_headerView= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RequestHeaderView" forIndexPath:indexPath];
[self.searchController.searchBar removeFromSuperview];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
[_headerView.searchView addSubview:self.searchController.searchBar];
return _headerView;
}
然而,这是非常肮脏的方法。我猜你正在将一些UIElement传递给command参数。这违反了viewmodel的责任。考虑将需要引用UIElement的代码移动到代码隐藏。
答案 1 :(得分:0)
只需将两个参数放入一个对象即可。您可以使用任何类型的集合或数组,但也许最简单的选择是使用IMultiValueConverter
中的Tuple<T1, T2>
Class:
if (values != null && values.Length >= 2)
{
Tuple<Type1, Type2> yourTwoValues = new Tuple<Type1, Type2>(values[0], values[1]);
return yourTwoValues;
}
然后,您可以将Tuple
作为参数传递给ICommand
,并在另一端提取单个值。
答案 2 :(得分:0)
尝试使用新属性,在CommandParameter中进行多重操作并在ExecuteEnterCommand中进行处理。比如object[] arr = (object[])obj;
public ICommand EnemyPopupTooltip
{
get
{
if (this.enemyPopupTooltip == null)
{
this.enemyPopupTooltip = new RelayCommand<object>(this.ExecuteEnterCommand, this.CanExecuteEnterCommand);
}
return this.enemyPopupTooltip;
}
}
private ICommand enemyPopupTooltip;