2014-04-09 25 views
0

我開發一個WPF應用程序,我面向.NET Framework 4的WPF綁定ValidatesOnException不要再追拋出:SecurityException

我有一個綁定到一個電子郵件屬性文本框和綁定有ValidatesOnException=True捕捉可能發生,而任何異常設置電子郵件屬性。

如果當前主體不在角色「編輯人員」中,則我的電子郵件屬性會引發安全異常。

當安全異常被拋出時,應用程序崩潰而不是突出顯示紅色的電子郵件文本框。

這是我的XAML對TextBox綁定:

<TextBox Text="{Binding Path=Email, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" /> 

這裏是我的email屬性代碼:

Public Property Email As String 
    Get 
     Return return _email 
    End Get 
    Set(value As String) 
     If Not Thread.CurrentPrincipal.IsInRole("Edit-Person") Then Throw New Security.SecurityException("You are not permitted to edit the email address") 
     _email = value 
    End Set 
End Property 

如果我把一個ValidationExceptionArgumentException結合捕獲異常,並且應用程序不會崩潰。這似乎只是SecurityException的一個問題。

根據Binding.ValidatesOnException Property的MSDN文檔,我不明白爲什麼這不起作用。

實現IDataErrorInfo接口不是我的選擇。

我真的可以在這裏使用一些建議。

編輯: 我創建了一個新的測試項目,以確保有沒有在我一起工作的一個奇怪的事情發生,發現相同的結果。

我創建了一個名爲WPFSecurityException測試世界糧食計劃署項目和複製從MSDN文檔的ValidationException財產粘貼的例子...並修改拋出是一個SecurityExcpetion例外。

例如,這裏是我的測試WPF項目的主窗口XAML:

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:src="clr-namespace:WPFSecurityExceptionTest" 
    Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Margin="20"> 
     <StackPanel.Resources> 
      <src:PersonThrowException x:Key="data"/> 

      <!--The tool tip for the TextBox to display the validation error message.--> 
      <Style x:Key="textBoxInError" TargetType="TextBox"> 
       <Style.Triggers> 
        <Trigger Property="Validation.HasError" Value="true"> 
         <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 

     </StackPanel.Resources> 
     <TextBlock>Enter your age:</TextBlock> 
     <TextBox Style="{StaticResource textBoxInError}"> 
      <TextBox.Text> 
       <!--By setting ValidatesOnExceptions to True, it checks for exceptions 
     that are thrown during the update of the source property. 
     An alternative syntax is to add <ExceptionValidationRule/> within 
     the <Binding.ValidationRules> section.--> 
       <Binding Path="Age" Source="{StaticResource data}" 
         ValidatesOnExceptions="True" 
         UpdateSourceTrigger="PropertyChanged" 
         > 
       </Binding> 
      </TextBox.Text> 
     </TextBox> 
     <TextBlock>Mouse-over to see the validation error message.</TextBlock> 
    </StackPanel> 
</Window> 

這裏是爲窗口和自定義PersonThrowException類的VB.NET代碼:

Class MainWindow 

End Class 
Public Class PersonThrowException 
    Private m_age As Integer 

    Public Property Age() As Integer 
     Get 
      Return m_age 
     End Get 
     Set(ByVal value As Integer) 

      If value < 0 OrElse value > 150 Then 
       Throw New Security.SecurityException("Age must not be less than 0 or greater than 150.") 
      End If 
      m_age = value 
     End Set 
    End Property 
End Class 
+0

你爲什麼要拼鬥的可執行文件?只要拋出一個ArgumentException。你想驗證吞下一般的異常嗎? – Paparazzi

+1

因爲當你對某些東西沒有權限時,它不是「ArugmentException」,它是一個安全異常。 – Frinavale

+1

沒有什麼可以表明SecurityExceptions不應該像其他類型的異常一樣被捕獲和處理。如果我拋出一個SystemException(SecuirtyException繼承的異常),它的處理正常...如果我拋出任何其他類型的異常,我可以認爲它處理得很好...什麼是SecurityException? – Frinavale

回答

0

您是否試過在發佈模式下構建代碼並從Visual Studio外部運行可執行文件?

從回答另一個問題:https://stackoverflow.com/a/5510097/4456875

的解決方案是不那麼明顯,也有據可查的,但也足夠簡單。在調試模式下運行時的原因的Visual Studio減免的例外,是因爲它的配置這樣

您可以配置調試 - >例外菜單此行爲或只是運行

+0

OP說程序崩潰了,而不是VS在第一次機會異常時中斷。這是兩件完全不同的事情。真正的答案是WPF是一個bugfest。 –

相關問題