2017-05-18 23 views
-1

這裏是我的XAML:組合框崩潰WPF應用程序,因爲的SelectionChanged是射擊太快

<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處理程序(通過評論的要求):

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; 
    } 

我的應用程序運行時,如果SelectedIndexSelectionChanged處理程序或ComboBoxItems被刪除。問題似乎是處理程序太快訪問cloneSelector並引發空對象異常。有沒有簡單的方法來防止這種行爲,同時仍然保持我的功能(即沒有SelectedIndex從-1開始)?另外,如果我沒有頻繁測試我的項目,我就不知道我做了什麼來打破它。它成功建立並失敗。當我調試,在無符號加載設計師異常未處理的盒子說

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拋出,但它沒有給我什麼是零或什麼導致了錯誤的信息。我如何獲得更好的調試信息,即讓它告訴我ComboBox是崩潰的根源?

+1

請將SelectionChanged處理程序添加到問題中。 – Yevgeniy

+1

哦,堅持下去。這是一個很好的電話。我認爲videoDevice實際上是空的。 –

+1

在調試期間,請確保「異常設置」視圖中的「公共語言運行時例外」複選框處於選中狀態。這將更容易調試。 – Seuleuzeuh

回答

0

事實證明,我的ComboBox甚至不是空引用,它是我的視頻分辨率對象。所有更多的理由我需要更具體的調試信息。