2012-04-02 27 views
1

驗證WPF 4.5中的異常嗎?我的疑問很簡單,我如何在WPF 4.5中使用此INotifyDataErrorInfo顯示異常?你知道如何使用INotifyDataErrorInfo

我使用MVVM:

這是我的看法

<TextBox MinHeight="50" 
      Text="{Binding Person.Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" 

在這裏,我的模型類的一部分。檢查驗證方法,在這裏我設置@字符,應該拋出一個異常

public class Person : DomainObject 
{ 
    private string _name; 

    public string Name 
    { 
     get 
     { 
      return this._name; 
     } 

     set 
     { 
      if (this._name != value) 
      { 
       this.ValidateProperty("Name", value); 
       this._name = value; 
       this.RaisePropertyChanged("Name"); 
      } 
     } 
    } 
} 

    protected override void ValidateProperty(string propertyName, object value) 
    { 
     if (propertyName == "Name") 
     { 
      var errors = new List<string>(); 

      var response = value as string; 

      if (string.IsNullOrEmpty(response)) 
      { 
       errors.Add("The value cannot be null or empty"); 
      } 
      else if (response == "@") 
      { 
       throw new Exception("@"); 
      } 

      this.ErrorsContainer.SetErrors(propertyName, errors); 
     } 
     else 
     { 
      base.ValidateProperty(propertyName, value); 
     } 
    } 

出現這種情況時,它真的停止節目。而據我所知,在Silverlight不會發生此。

+0

您能否提供更多的上下文? – 2012-04-02 22:16:54

+0

@TravisJ信息添加! – 2012-04-02 22:23:48

回答

1

除了綁定之外,您可能還會在其他地方使用setter(並且您沒有發現異常)。

您需要在調試模式下運行您的應用程序。發生異常時,Visual Studio會向您顯示異常助手。

然後,您將能夠分析堆棧跟蹤並查看程序如何調用此代碼。

如果它不能解決您的問題,請更新您的問題與異常的堆棧跟蹤(Visual Studio稱之爲「未處理的異常」),導致您的程序停止。

相關問題