2013-10-10 110 views
0

我有一個公共Event EventChanged的抽象基類。我有一個由另一個孩子班組成的孩子班,我想把這個活動傳播給所有者。但是當我試圖把事件聯繫起來時,我什麼也得不到。事件似乎沒有正確連接

public abstract class ViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChange(string propertyName) 
    { 
     if (this.PropertyChanged == null) return; 

     this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public class Customer : ViewModel 
{ 
    public Address Address 
    { 
     get { return _Address; } 
     set 
     { 
      _Address = value; 
      OnPropertyChanged("Address"); 
     } 
    } 
    private Address _Address = new Address(); 

    public Customer() 
    { 
     // I get nothing here. But why? 
     Address.PropertyChanged += (o, e) => Logger.Log("Just do something, please!"); 

     // What I want to do is get Customer propertychange to fire 
     // Because currently Address changes are not detected. 
    } 
} 

public class Address : ViewModel 
{ 
    private string _addy = ""; 
    public string Addy 
    { 
     get { return _addy; } 
     set 
     { 
      _addy=value; 
      Logger.Log("Testing that at least something works"); 
      // I have verified that this is getting called, firing the event. 
      OnPropertyChange("Addy"); 
     } 
    } 
} 

編輯:UPDATE:

對於那些誰是好奇(我認爲這是你的,Servy)。問題原來是地址在代碼的另一部分得到重置。所以實際上,掛鉤在構造函數中按預期發生,然後立即丟失,因爲Address被設置爲不同的實例。

我很欣賞這些迴應。它幫助我縮小了問題不是(這不是語法錯誤)。它還幫助我瞭解如何在未來發布更好的問題。通過在困難的情況下發布問題,我能夠追蹤到一個簡單而難以回答的答案。謝謝。你的貢獻非常有幫助。

但是,我認爲這個問題演示了具體問題,並且問題是並且不斷更新以滿足任何關於可編譯性的擔憂(上面的代碼工作得很好)。關於代碼的兩件事情。每次有人擔心時,我都會更新它。 2.我已經使用過這個站點很多次了,而且那些能夠真正分辨像這個只需要僞代碼來演示的概念性問題之間的區別的人就能從中受益。所以我不知道使用僞代碼會導致這樣的騷動。真誠的歉意。我將來會更加小心。

我認爲這個問題演示了具體問題因爲我的具體問題是我無法弄清楚爲什麼事件沒有掛鉤,雖然它是射擊。這個問題被Servy精確地解決了(這不是一個語法錯誤),Wonko完全解決了這個問題(邏輯錯誤是因爲Address被覆蓋),但是我願意更多地瞭解什麼適合堆棧溢出,什麼不是。

在這一點上,我很樂意將這個話題關閉,因爲它似乎是一些人的問題。但我不知道如何關閉它。一旦我意識到這是什麼大不了的一些,我試着刪除它,而是讓你的代碼編譯了我的一切工作正常,你不能評論:(刪除主題

+3

請修正語法錯誤的代碼,以便它實際上*編譯*第一。實際上有很多。 – Servy

+0

當我修復語法錯誤並確保'Customer'構造在'Customer'構造函數中時,它工作得很好,就像我想的那樣。 – Servy

+1

@nuke實際上有幾個。有這樣的事實,並且'Address'具有與其類型相同的名稱的成員,並且'class'在所有類定義(現在已編輯)中都缺少,所以當事件發生時不會爲每個客戶構建一個Address。被添加,導致NRE,並且'_addy'被作爲'addy'訪問(缺少下劃線)。 – Servy

回答

2

稍作修改:

[STAThread] 
     static void Main() 
     {   
      Customer cust = new Customer(); 
      cust.Address.Address1 = "HAllo"; 
     } 

     public abstract class ViewModel : INotifyPropertyChanged 
     { 
      public event PropertyChangedEventHandler PropertyChanged; 

      protected void OnPropertyChange(string propertyName) 
      { 
       if (PropertyChanged == null) return; 

       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     public class Customer : ViewModel 
     { 
      public Address Address { get; set; } //this implements inpc but I don't show that here. 
      public Customer() 
      { 
       Address = new Address(); 
       // I get nothing here. But why? 
       Address.PropertyChanged += (o, e) => Console.WriteLine("Just do something, please!"); 

       // What I want to do is get Customer propertychange to fire 
       // Because currently Address changes are not detected. 
      } 
     } 

     public class Address : ViewModel 
     { 
      private string _addy = ""; 
      public string Address1 
      { 
       get { return _addy; } 
       set 
       { 
        _addy = value; 
        Console.WriteLine("Testing that at least something works"); 
        // I have verified that this is getting called, firing the event. 
        OnPropertyChange("Addy"); 
       } 
      } 
     } 

請接受我的建議上面閱讀一些有關debugging嚴重。

1

當我知道你想你的客戶類射擊每當它的地址觸發事件OnPropertyChanged事件。如果是這樣,你需要更改訂閱的客戶以下級別:

public Customer : ViewModel 
{ 
    public Address Address {get; set;} //this implements inpc but I don't show that here. 
    public Customer() 
    { 
     // I get nothing here. But why? 
     Address.PropertyChanged += (o, e) => OnPropertyChange(e.PropertyName); 

     // What I want to do is get Customer propertychange to fire 
     // Because currently Address changes are not detected. 
    } 
} 

什麼你基本上做的是重播事件火

+0

但是,如果他現有的事件處理程序沒有記錄,那麼這個變化就沒有關係。只是日誌記錄的目的是爲了簡化示例代碼,並在它到達這個點之前顯示它失敗*。 – Servy

+0

所以你甚至沒有達到Address.PropertyChanged + =(o,e)=> Log .....?你能否顯示修改客戶地址屬性的代碼? –

+0

這不是我的問題,但OP表明'Address.PropertyChanged'處理程序沒有被執行;日誌條目不存在。 – Servy