xamarin - 使用 try catch 自定义 Xamarin.Forms.Command
问题描述
我已经复制了Xamarin.Forms.Command,并在 Execute 方法中添加了一个尝试捕获,当我的 InvalidAppVersionException 发生时发布特定事件(使用 EventAggregator de Prism)。
try catch 不起作用,我不明白为什么。任何人都可以帮助我吗?
我的自定义命令代码:
public class LudaAppCommand : ICommand
{
readonly IEventAggregator _eventAggregator;
readonly Func<object, bool> _canExecute;
readonly Action<object> _execute;
public LudaAppCommand(Action<object> execute, IEventAggregator eventAggregator)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_eventAggregator = eventAggregator;
}
public LudaAppCommand(Action execute, IEventAggregator eventAggregator) : this(o => execute(), eventAggregator)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
}
public LudaAppCommand(Action<object> execute, Func<object, bool> canExecute, IEventAggregator eventAggregator) : this(execute, eventAggregator)
{
if (canExecute == null)
throw new ArgumentNullException(nameof(canExecute));
_canExecute = canExecute;
}
public LudaAppCommand(Action execute, Func<bool> canExecute, IEventAggregator eventAggregator) : this(o => execute(), o => canExecute(), eventAggregator)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
if (canExecute == null)
throw new ArgumentNullException(nameof(canExecute));
}
public bool CanExecute(object parameter)
{
if (_canExecute != null)
return _canExecute(parameter);
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
// TODO: Este try catch no funciona :(
try
{
_execute(parameter);
}
catch (Exceptions.InvalidAppVersionException)
{
_eventAggregator.GetEvent<Events.InvalidAppVersionEvent>().Publish();
}
}
public void ChangeCanExecute()
{
EventHandler changed = CanExecuteChanged;
changed?.Invoke(this, EventArgs.Empty);
}
}
解决方案
推荐阅读
- django - 保存新的 django 模型对象会给出重复的键
- c# - 如何在设计时为 UserControl 显示 & 符号?
- java - Java 文本字段输入问题
- ruby-on-rails - 如何使用 mysql@5.6 修复“无法加载身份验证插件'caching_sha2_password'”
- python - 如何优化大型pyspark数据帧中的百分比检查和cols drop?
- python - 如何在 C++ 中使用 ZeroMQ 通信多个图像?
- spring-boot - spirng-boot 如何在自定义 spring-boot-start 中添加拦截器
- typescript - 如何获得将返回值类型缩小到特定子类型而不是返回联合或索引类型的函数
- haskell - 在 Haskell 中难以实现 Huffman 树
- assembly - 如何正确编写文件读取的字符串?