1

我的疑问很简单,如何在 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 中不会发生这种情况。

4

1 回答 1

1

您可能在绑定之外的其他地方使用您的设置器(并且您没有捕获异常)。

您需要在调试模式下运行您的应用程序。发生异常时,Visual Studio 将向您显示异常助手。

然后您将能够分析堆栈跟踪并查看您的程序如何调用此代码。

如果它不能解决您的问题,请使用导致您的程序停止的异常(Visual Studio 将其称为“未处理的异常”)的堆栈跟踪更新您的问题。

于 2012-04-02T22:33:40.120 回答