UWP:RelayCommand 上的 Int 参数失败
UWP: Int parameter fails on RelayCommand
这段带有字符串参数的代码有效:
public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));
private void Test(string s)
{
//DoSomething();
}
此带有 int 参数的代码不起作用并给出 Compiler Error CS0452:
public RelayCommand<int> myCommand { get; private set; }
myCommand = new RelayCommand<int>((s) => Test(s));
private void Test(int s)
{
//DoSomething();
}
RelayCommand Class 与 xaml 上的命令绑定 UI:
public class RelayCommand<TParameter> : System.Windows.Input.ICommand
where TParameter : class
{
protected readonly System.Func<TParameter, bool> _canExecute;
private readonly System.Action<TParameter> _execute;
public RelayCommand(System.Action<TParameter> execute)
: this(execute, null)
{
}
public RelayCommand(System.Action<TParameter> execute, System.Func<TParameter, bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter as TParameter);
}
public void Execute(object parameter)
{
_execute(parameter as TParameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
System.EventHandler handler = CanExecuteChanged;
if (handler != null)
{
handler(this, System.EventArgs.Empty);
}
}
}
除了将其转换为对象类型之外,还有其他想法吗?
如果您希望能够将 RelayCommand<TParameter>
实现与 int
等值类型一起使用,您需要删除类型约束并使用 as
运算符:
public class RelayCommand<TParameter> : System.Windows.Input.ICommand
{
protected readonly System.Func<TParameter, bool> _canExecute;
private readonly System.Action<TParameter> _execute;
public RelayCommand(System.Action<TParameter> execute)
: this(execute, null)
{
}
public RelayCommand(System.Action<TParameter> execute, System.Func<TParameter, bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
if (parameter is TParameter)
return _canExecute((TParameter)parameter);
return false;
}
public void Execute(object parameter)
{
if (parameter is TParameter)
_execute((TParameter)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
System.EventHandler handler = CanExecuteChanged;
if (handler != null)
{
handler(this, System.EventArgs.Empty);
}
}
}
这段带有字符串参数的代码有效:
public RelayCommand<string> myCommand { get; private set; }
myCommand = new RelayCommand<string>((s) => Test(s));
private void Test(string s)
{
//DoSomething();
}
此带有 int 参数的代码不起作用并给出 Compiler Error CS0452:
public RelayCommand<int> myCommand { get; private set; }
myCommand = new RelayCommand<int>((s) => Test(s));
private void Test(int s)
{
//DoSomething();
}
RelayCommand Class 与 xaml 上的命令绑定 UI:
public class RelayCommand<TParameter> : System.Windows.Input.ICommand
where TParameter : class
{
protected readonly System.Func<TParameter, bool> _canExecute;
private readonly System.Action<TParameter> _execute;
public RelayCommand(System.Action<TParameter> execute)
: this(execute, null)
{
}
public RelayCommand(System.Action<TParameter> execute, System.Func<TParameter, bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter as TParameter);
}
public void Execute(object parameter)
{
_execute(parameter as TParameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
System.EventHandler handler = CanExecuteChanged;
if (handler != null)
{
handler(this, System.EventArgs.Empty);
}
}
}
除了将其转换为对象类型之外,还有其他想法吗?
如果您希望能够将 RelayCommand<TParameter>
实现与 int
等值类型一起使用,您需要删除类型约束并使用 as
运算符:
public class RelayCommand<TParameter> : System.Windows.Input.ICommand
{
protected readonly System.Func<TParameter, bool> _canExecute;
private readonly System.Action<TParameter> _execute;
public RelayCommand(System.Action<TParameter> execute)
: this(execute, null)
{
}
public RelayCommand(System.Action<TParameter> execute, System.Func<TParameter, bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
if (parameter is TParameter)
return _canExecute((TParameter)parameter);
return false;
}
public void Execute(object parameter)
{
if (parameter is TParameter)
_execute((TParameter)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
System.EventHandler handler = CanExecuteChanged;
if (handler != null)
{
handler(this, System.EventArgs.Empty);
}
}
}