我有一个 VSTO 应用程序,它使用 Windows 窗体用户控件来承载 WPF 窗体。
我需要实现全局异常处理。
尝试从顶级用户控件处理 AppDomain.CurrentDomain.UnhandledException,但在引发异常时永远不会调用该函数。
任何指导在这种情况下,我在哪里/如何实现全局异常处理?
最佳答案
这是我在项目中使用的 VB.NET 示例。我使用一个类进行错误处理。
错误处理程序类
Public Class ErrorHandler
Public Shared Sub DisplayMessage(ex As Exception)
Dim sf As New System.Diagnostics.StackFrame(1)
Dim caller As System.Reflection.MethodBase = sf.GetMethod()
Dim currentProcedure As String = (caller.Name).Trim()
Dim errorMessageDescription As String = ex.ToString()
Dim msg As String = "Contact your system administrator. " + Environment.NewLine
msg += (Convert.ToString("Procedure: ") & currentProcedure) + Environment.NewLine
msg += "Description: " + ex.ToString() + Environment.NewLine
MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
End Class
使用示例
Public Class Ribbon
Public Sub YourProcedure()
Try
'try stuff here...
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Finally
End Try
End Sub
End Class
关于wpf - VSTO 全局异常处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48504639/