代码导致WPF Designer崩溃,为什么?

时间:2014-02-18 15:36:22

标签: c# wpf visual-studio-2012

我创建了一个类来监视更改的属性并触发INotifyPropertyChanged事件

然而,当一个类使用它添加到WPF控件时,设计器崩溃时会出现未处理的异常 System.Runtime.Remoting.RemotingException [4484]设计师流程意外终止!

谁知道为什么?

public interface IObservableValue<T>:INotifyPropertyChanged, INotifyPropertyChanging
{
    T Value { get; }
}

public class ObservableProperty<T> : IObservableValue<T>
{
    public static implicit operator T(ObservableProperty<T> obj)
    {
        return obj.Value;
    }

    public ObservableProperty()
        :this(default(T))
    {
    }
    public ObservableProperty(T value)
    {
        val = value;
        CheckInterface(val, true);
    }


    T val;

    public T Value
    {
        get { return val; }
        set
        {
            if (!val.Equals(value))
            {
                OnPropertyChanging(changingArgs);

                CheckInterface( val, false);
                val = value;
                CheckInterface( val, true);

                OnPropertyChanged(changedArgs);
            }
        }
    }

    public bool HasValue
    {
        get { return val!=null; }
    }


    protected void CheckInterface<TValue>(TValue value, bool add)
    {
        INotifyPropertyChanging inc = value as INotifyPropertyChanging;
        if (inc != null)
        {
            if (add)
                inc.PropertyChanging += new PropertyChangingEventHandler(val_PropertyChanging);
            else
                inc.PropertyChanging -= new PropertyChangingEventHandler(val_PropertyChanging);
        }

        INotifyPropertyChanged inpc = value as INotifyPropertyChanged;
        if (inpc != null)
        {
            if (add)
                inpc.PropertyChanged += new PropertyChangedEventHandler(val_PropertyChanged);
            else
                inpc.PropertyChanged -= new PropertyChangedEventHandler(val_PropertyChanged);
        }
        INotifyCollectionChanged incc = value as INotifyCollectionChanged;
        if (incc != null)
        {
            if (add)
                incc.CollectionChanged += new NotifyCollectionChangedEventHandler(val_CollectionChanged);
            else
                incc.CollectionChanged -= new NotifyCollectionChangedEventHandler(val_CollectionChanged);
        }
    }


    void val_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged(changedArgs);
    }
    void val_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
        OnPropertyChanging(changingArgs);
    }
    void val_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged(changedArgs);
    }
    void OnPropertyChanged(PropertyChangedEventArgs changed)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, changed);
    }
    void OnPropertyChanging(PropertyChangingEventArgs changed)
    {
        var handler = PropertyChanging;
        if (handler != null) handler(this, changed);
    }


    private static PropertyChangedEventArgs changedArgs = new PropertyChangedEventArgs("Value");
    private static PropertyChangingEventArgs changingArgs = new PropertyChangingEventArgs("Value");

    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

}

2 个答案:

答案 0 :(得分:2)

我唯一看到的就是这一行

if(!val.Equals(value))
NullReferenceException为空时,

将抛出val。另外,我敢打赌设计师使用类的默认构造函数,这意味着设计器中val为空,因此值设置器会抛出异常。

答案 1 :(得分:0)

非常奇怪,但重启VS2012修复了它