为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
HUX布斯 2021-11-14 10:21:20
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 或手动添加信息以跟踪引发异常的位置?(至少是哪个类/控件/页面/窗口,甚至可能是哪个绑定)
查看完整描述

2 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

我怀疑你最好的办法是通过内存转储来诊断问题。如何获得一个,取决于你的异常处理代码是如何设置的。

如果您的应用程序本身处理异常(并且不会崩溃),则很难生成转储。您可以尝试此处概述的方法:自动生成 .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 ) .


查看完整回答
反对 回复 2021-11-14
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

我在 App.xaml.cs 文件中做了类似下面的事情


  /// <summary>

/// Interaction logic for App.xaml

/// </summary>

public partial class App : Application

{

    static string filePath = System.IO.Path.Combine(Environment.GetFolderPath(

    Environment.SpecialFolder.ApplicationData), "Log.txt");

    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)

    {

        using (StreamWriter writer = new StreamWriter(filePath, true))

        {

          writer.WriteLine("Message :" + e.Exception.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.Exception.StackTrace +  "" + Environment.NewLine + "Date :" + Now.ToString());

          writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);

        }

        MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);

        e.Handled = true;

    }

}

希望它会帮助你


查看完整回答
反对 回复 2021-11-14
  • 2 回答
  • 0 关注
  • 298 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信