我目前正在製作一個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
方法不會被調用。有任何想法嗎?
'handler == null'顯然是一個錯誤(打字錯誤)。 – Sinatr