Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有try-catch的自定义Xamarin.Forms.Command_Xamarin_Xamarin.forms_Command_Try Catch_Prism - Fatal编程技术网

带有try-catch的自定义Xamarin.Forms.Command

带有try-catch的自定义Xamarin.Forms.Command,xamarin,xamarin.forms,command,try-catch,prism,Xamarin,Xamarin.forms,Command,Try Catch,Prism,我复制了,并在Execute方法中添加了一个try-catch,当我的InvalidAppVersionException发生时,它会发布一个特定的事件(使用EventAggregator de Prism) 试抓不起作用,我不明白为什么。 有人能帮我吗 我的自定义命令代码: public class LudaAppCommand : ICommand { readonly IEventAggregator _eventAggregator; readonly Func<o

我复制了,并在Execute方法中添加了一个try-catch,当我的InvalidAppVersionException发生时,它会发布一个特定的事件(使用EventAggregator de Prism)

试抓不起作用,我不明白为什么。 有人能帮我吗

我的自定义命令代码:

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);
    }
}
公共类LudaAppCommand:ICommand
{
只读IEventagegrator\u事件聚合器;
只读功能可执行;
只读操作_执行;
公共LudaAppCommand(操作执行,IEventAggregator事件聚合器)
{
if(execute==null)
抛出新ArgumentNullException(nameof(execute));
_执行=执行;
_eventAggregator=eventAggregator;
}
public LudaAppCommand(Action execute,IEventAggregator eventAggregator):这个(o=>execute(),eventAggregator)
{
if(execute==null)
抛出新ArgumentNullException(nameof(execute));
}
public LudaAppCommand(Action execute,Func canExecute,IEventAggregator eventAggregator):这个(execute,eventAggregator)
{
如果(canExecute==null)
抛出新ArgumentNullException(nameof(canExecute));
_canExecute=canExecute;
}
公共LudaAppCommand(Action execute、Func canExecute、IEventAggregator eventAggregator):这(o=>execute()、o=>canExecute()、eventAggregator)
{
if(execute==null)
抛出新ArgumentNullException(nameof(execute));
如果(canExecute==null)
抛出新ArgumentNullException(nameof(canExecute));
}
公共布尔CanExecute(对象参数)
{
如果(_canExecute!=null)
返回_canExecute(参数);
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
//TODO:Este try catch no funciona:(
尝试
{
_执行(参数);
}
捕获(异常。InvalidAppVersionException)
{
_eventAggregator.GetEvent().Publish();
}
}
public void ChangeCanExecute()
{
EventHandler changed=CanExecuteChanged;
已更改?.Invoke(此为EventArgs.Empty);
}
}

尝试向传递到
public LudaAppCommand(Action execute,…)的操作中添加一个Try catch
是的,但这意味着在作为操作传递给命令构造函数的每个函数中添加相同的try-catch。我正在寻找一种通用方法来通过我的所有代码处理此异常。我们如何知道它不起作用?编写一个单元测试并在此处共享如何?