4

我想我发现了 RescueAttribute 被破坏的情况。或者我可能不正确地使用协同程序。

我有一个这样的 ViewModel:

[Rescue("Rescue")]
class MyViewModel
{
    //... left out some bus-logic code here ...

    public void Login()
    {
        yield return Show.Busy();

        //the following line will also cause the problem, just like AsyncResult
        //yield return Show.MessageBox("Test");

        yield return new AsyncResult(() => _server.Login());

        //throw new Exception("Aww, snap!");

        yield return Show.NotBusy();
    }

    public void Rescue(Exception exc)
    {
        //Show a messagebox or something
    }
}

AsyncResult 是这样实现的:

using Exec = Caliburn.PresentationFramework.Invocation.Execute;

    public class AsyncResult : IResult
    {
        private readonly Action _function;

        public AsyncResult(Action function)
        {
            _function = function;
        }

        public void Execute(ResultExecutionContext context)
        {
            Exec.OnBackgroundThread(delegate
            {
                try
                {
                    _function();
                }
                catch (Exception exc)
                {
                    Exec.OnUIThread(() => Completed(this, new ResultCompletionEventArgs { Error = exc, WasCancelled = true }));
                    return;
                }
                Exec.OnUIThread(() => Completed(this, new ResultCompletionEventArgs()));
            });
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
    }

如果我在上面的 ViewModel 中取消注释异常,Rescue 将无法处理异常。

这是 Caliburn 中的错误,还是 AsyncResult 实施错误?

如果您在 yield 之前放置一个异常以返回 AsyncResult,Rescue 就可以正常工作。此外,如果在异步线程上引发异常,救援仍然有效!

编辑:您也可以使用 Show.MessageBox 而不是 AsyncResult 来重现相同的问题。

4

2 回答 2

3

这似乎是一个合法的错误。我在 Caliburn 跟踪器中为此添加了一个问题:http: //caliburn.codeplex.com/workitem/7636

编辑:问题已解决,
请参阅:http ://caliburn.codeplex.com/Thread/View.aspx?ThreadId=234229

于 2010-11-10T22:11:36.500 回答
0

我建议对 IDispatcher 进行更改以适用于此问题:

http://caliburn.codeplex.com/Thread/View.aspx?ThreadId=223873

于 2010-11-11T01:24:43.970 回答