ComboBox崩溃WPF应用程序,因为SelectionChanged触发得太早

编程入门 行业动态 更新时间:2024-10-19 14:52:08
本文介绍了ComboBox崩溃WPF应用程序,因为SelectionChanged触发得太早的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的XAML:

<ComboBox x:Name="cloneSelector" Width="80" SelectedIndex="0" VerticalAlignment="Top" Margin="10" SelectionChanged="cloneSelector_SelectionChanged"> <ComboBoxItem Content="All" /> <ComboBoxItem Content="Top" /> <ComboBoxItem Content="Middle" /> <ComboBoxItem Content="Bottom" /> <ComboBoxItem Content="None" /> </ComboBox>

这是SelectionChanged处理程序(按评论要求):

And here is the SelectionChanged handler (as requested by a comment):

cloneSelection = (CloneFormat) cloneSelector.SelectedIndex; var frameSize = videoDevice.VideoResolution.FrameSize; switch (cloneSelection) { case CloneFormat.Top: cloneRect = new Rectangle(0, 0, frameSize.Width, (frameSize.Width* 9) / 16); break; case CloneFormat.Middle: cloneRect = new Rectangle(0, 100, frameSize.Width, (frameSize.Width* 9) / 16); break; case CloneFormat.Bottom: cloneRect = new Rectangle(0, 200, frameSize.Width, (frameSize.Width* 9) / 16); break; default: cloneRect = new Rectangle(0, 0, frameSize.Width, frameSize.Height); break; }

如果 SelectedIndex , SelectionChanged 处理程序或 ComboBoxItems 被删除。问题似乎是处理程序过早访问 cloneSelector 并抛出空对象异常。有没有简单的方法可以在保持我的功能的同时防止这种行为(即,SelectedIndex从-1开始)?

My application runs if SelectedIndex, the SelectionChanged handler, or the ComboBoxItems are removed. The problem seems to be that the handler is accessing cloneSelector too soon and throwing a null object exception. Is there an easy way to prevent this behavior while still maintaining my functionality (i.e. not having SelectedIndex start at -1)?

此外,如果我没有测试我的项目所以经常我不知道该怎么做才能打破它。它可以成功构建,并且可以静默失败。调试时,未加载任何符号设计器中的未处理的异常框显示

Also, if I didn't test my project so frequently I would've had no idea what I did to break it. It builds successfully and fails silently. When I debug, the Exception Unhandled box in the No Symbols Loaded designer says

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' Inner Exception NullReferenceException: Object reference not set to an instance of an object.

我可以看到它是在PresentationFramework.dll中抛出的,但它没有提供有关什么的信息null或导致错误的原因。如何获得更好的调试信息,即告诉我 ComboBox 是崩溃的根源?

And I can see that it was thrown in PresentationFramework.dll but it gives me no information about what was null or what's causing the error. How do I get better debugging info, i.e. get it to tell me that the ComboBox is the source of the crash?

推荐答案

原来,我的 ComboBox 甚至不是空引用,而是我的视频分辨率对象。更多原因是我需要更具体的调试信息。

It turns out my ComboBox wasn't even the null reference, it was my video resolution object. All the more reason I need more specific debugging info.

更多推荐

ComboBox崩溃WPF应用程序,因为SelectionChanged触发得太早

本文发布于:2023-11-27 08:23:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:太早   应用程序   ComboBox   WPF   SelectionChanged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!