猿问

查找抛出的 WPF 异常的类/行号

System.ArgumentOutOfRangeException由于用于访问 a 中的项目的错误索引DataGrid(发生在已发布的版本上并且没有进行用户交互)而引发了A 。


遵循我们收到的堆栈跟踪的一部分:


System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Parameter name: index

   at System.Windows.Media.VisualCollection.get_Item(Int32 index)

   at System.Windows.Controls.UIElementCollection.get_Item(Int32 index)

   at System.Windows.Controls.UIElementCollection.System.Collections.IList.get_Item(Int32 index)

   at System.Windows.Controls.DataGridCellsPanel.ArrangeOverride(Size arrangeSize)

   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)

   at System.Windows.UIElement.Arrange(Rect finalRect)

   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)

   at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)

   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)

   [...]

在堆栈跟踪的进一步下方,我们还可以看到虚拟化DataGrid在引发异常的地方处于活动状态。该错误很可能与延迟加载/虚拟化有关,但是,它发生在哪里仍然是个谜。


是否可以配置 WPF 或手动添加信息以跟踪引发异常的位置?(至少是哪个类/控件/页面/窗口,甚至可能是哪个绑定)


HUX布斯
浏览 361回答 2
2回答

呼唤远方

我怀疑你最好的办法是通过内存转储来诊断问题。如何获得一个,取决于你的异常处理代码是如何设置的。如果您的应用程序本身处理异常(并且不会崩溃),则很难生成转储。您可以尝试此处概述的方法:自动生成 .NET 故障转储。或者,如果您在抛出异常时让应用程序保持活动状态(使用对话框或其他东西),您可以要求用户获取内存转储以进行进一步分析(通过任务管理器 => 右键单击进程 => 创建转储) .如果您的应用程序真的崩溃了,可以自动创建一个转储文件,但这需要在注册表中进行配置(参见https://blogs.msdn.microsoft.com/tess/2010/08/23/getting-full-user -mode-dumps-automatically-when-your-process-crashes/ , https://www.meziantou.net/2018/06/04/tip-automatically-create-a-crash-dump-file-on-error ) .

牛魔王的故事

我在 App.xaml.cs 文件中做了类似下面的事情&nbsp; /// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : Application{&nbsp; &nbsp; static string filePath = System.IO.Path.Combine(Environment.GetFolderPath(&nbsp; &nbsp; Environment.SpecialFolder.ApplicationData), "Log.txt");&nbsp; &nbsp; private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (StreamWriter writer = new StreamWriter(filePath, true))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine("Message :" + e.Exception.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.Exception.StackTrace +&nbsp; "" + Environment.NewLine + "Date :" + Now.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);&nbsp; &nbsp; &nbsp; &nbsp; e.Handled = true;&nbsp; &nbsp; }}希望它会帮助你
随时随地看视频慕课网APP
我要回答