ComboBox崩溃WPF应用程序,因为SelectionChanged触发得太早 [英] ComboBox crashing WPF application because SelectionChanged is firing too soon

查看:82
本文介绍了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触发得太早的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆