事件未调用C#



我目前正在制作NFC库,当成功读取卡时,我需要在其中引发事件。

在我的主项目中,我这样称呼图书馆:

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方法。有什么想法吗?

我想您检查处理程序是否为null是一个拼写错误?;)

也许Initialize确实返回false。调试器之所以能够逐步完成,正是因为它不会调用任何东西。这是由if.确定的

我建议您使用handler!=null,并确保Initialize返回true。

相关内容

  • 没有找到相关文章

最新更新