簡體   English   中英

您是否知道如何使用INotifyDataErrorInfo在WPF 4.5中驗證異常

[英]Does you know how to validate an exception in WPF 4.5 using INotifyDataErrorInfo

我的疑問很簡單,如何在WPF 4.5中使用此INotifyDataErrorInfo顯示異常?

我正在使用MVVM:

這是我觀點的一部分

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

這是我的模型課。 檢查Validate方法,我在其中設置@字符,應該拋出異常

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中不會發生這種情況。

您可能在綁定以外的其他地方使用了setter(並且您沒有捕獲到異常)。

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

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

如果不能解決問題,請使用導致程序停止的異常的堆棧跟蹤(Visual Studio稱為“未處理的異常”)更新您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM