问题
Here's my 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>
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;
}
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.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/44051514/combobox-crashing-wpf-application-because-selectionchanged-is-firing-too-soon