【问题标题】:How to get more info on AnimationException?如何获取有关 AnimationException 的更多信息?
【发布时间】:2020-04-24 23:57:24
【问题描述】:

我有一个 WPF 应用程序,我在其中创建了一个 Storyboard 来为一些控件设置动画。触发动画后,项目中断,但我从 Visual Studio 得到的只是以下输出消息:

Exception thrown: 'System.Windows.Media.Animation.AnimationException' in WindowsBase.dll

如何获得更多信息以便我确切知道问题出在哪里?该文档仅声明

在制作动画时发生错误时引发的异常 属性。

我不知道代码在哪一行插入try-catch 块。

【问题讨论】:

  • 您在情节提要中创建动画。那是一个你然后开始的对象。除非您编写了自定义动画,否则代码中不会有一行返回错误。你在什么上使用什么动画?
  • 我确实写了一个自定义动画@Andy。故事板包括DoubleAnimationUsingPathDoubleAnimation。我将其应用于普通控件。

标签: c# wpf animation exception try-catch


【解决方案1】:

首先我会像这样捕获这些未处理的异常:

public MyCustomControl()
{
  Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}

void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
  switch (e.Exception)
  {
    case AnimationException aniEx:
      Log(aniEx.GetDetails());
      break;
    // ...
    // default: ...
  }
}

其次,我会为异常添加一个 GetDetails 扩展,以便从中获取所有详细信息:

public static class Extensions
{
    /// <summary>
    /// Returns all Details of an exception
    /// </summary>
    /// <param name="exception"></param>
    /// <returns></returns>
    public static string GetDetails(this Exception exception)
    {
        string GetInnerExceptionDetails(Exception e)
        {
            var s = new StringBuilder(e.GetType().FullName);
            if (e.GetType().GetProperties()
                 .Select(prop => new { prop.Name, Value = prop.GetValue(e, null) })
                 .Select(x => $"{x.Name}: {x.Value ?? string.Empty}").ToList() is List<string> props && props.Any())
            {
                s.AppendLine(string.Join(Environment.NewLine, props));
                s.AppendLine();
            }

            if (e.InnerException != null)
                s.AppendLine(GetInnerExceptionDetails(e.InnerException));
            return s.ToString();
        }
        return GetInnerExceptionDetails(exception);
    }
}

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 2017-06-15
    • 2015-10-14
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多