2014-02-18 63 views
0

我創建的類來監視更改屬性和觸發事件INotifyPropertyChanged的代碼導致WPF設計器崩潰,爲什麼?

然而,當使用一個類其加入到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; 

} 

回答

0

非常奇怪,但重新啓動VS2012修正了它

2

我唯一看到的是,這條線

if(!val.Equals(value)) 

將拋出一個NullReferenceExceptionval爲空。另外,我敢打賭,設計師使用該類的默認構造函數,這意味着val在設計器中爲null,因此值設置器會引發異常。

+1

等於不應該用在仿製藥的上下文中。而是使用'EquatabilityComparer .Default.Compare'。 – Georg

+0

比較在該課程中不存在,你的意思是等於? – MikeT

+0

然而,這不可能是觸發代碼的東西,它必須綁定到值屬性的setter,默認的構造函數直接設置屬性不使用屬性邏輯,它是在將該對象添加到控件資源字典中。在這種情況下,泛型類型也是雙倍的,所以null不是可能的值。因爲關閉VS2012的每一個打開的副本似乎已經治好了它,我認爲它一定是設計師特有的東西 – MikeT