2016-01-07 73 views
-1

我目前正在製作一個NFC圖書館,當我成功讀取一張卡片時,我需要舉辦一個活動。事件不叫C#

在我MainProject我調用庫這樣的:

reader = new NfcReader(); 
    if (reader.Initialize()) 
    { 
     reader.UidReceived += (s, args) => 
      DisplayText(String.Format("UID received: {0}", args.Uid)); 
    } 

我的圖書館:

public class NfcReader 
{ 
    private SCardMonitor monitor; 
    public event EventHandler<NfcReaderEventArgs> UidReceived; 

    public bool Initialize() 
    { 
     // When a card is inserted, an event is raised 
     // then I want to read the Uid of the card 
     monitor = new SCardMonitor(new SCardContext(), SCardScope.System); 
     monitor.CardInserted += (s,a) => GetUid(a.ReaderName); 
     return true; 
    } 

    private void GetUid(string readerName) 
    { 
     string uid = MyUidGetter(readerName); 
     OnUidReceived(uid); 
    } 

    private void OnUidReceived(string uid) 
    { 
     var handler = UidReceived; 
     if (handler == null) 
     { 
      handler(this, new NfcReaderEventArgs(uid)); 
     } 
    } 
} 

通過handler(this, new NfcReaderEventArgs(uid));調試步驟,但DisplayText方法不會被調用。有任何想法嗎?

+0

'handler == null'顯然是一個錯誤(打字錯誤)。 – Sinatr

回答

2

我想這是一個錯誤,你檢查處理程序爲空? ;)

也許你的Initialize返回false。調試器完全通過,因爲它不會調用任何東西。這是由if確定的。

我建議您使用handler!= null來確保Initialize返回true。

+1

哎呀......就是這樣。好吧,我想喝咖啡休息時間^^ –

+0

是啊!錯字一直在發生,很容易忽略。 – dryman