C# 如何在防止WP启动当前存储在NFC标记上的操作的同时写入NFC标记

C# 如何在防止WP启动当前存储在NFC标记上的操作的同时写入NFC标记,c#,windows-store-apps,windows-phone-8.1,nfc,C#,Windows Store Apps,Windows Phone 8.1,Nfc,我试图允许人们使用我的应用程序写入NFC标签,这样我的应用程序就可以使用自定义参数启动。我想能够重新编程的NFC标签上已经有数据 我正在使用下面的代码,但问题是,WP总是识别NFC标记上已经存在的操作并中断,因为它希望启动之前任何时候编写的NFC标记操作 我如何告诉操作系统停止触发标记的操作,以便我可以立即重写它 public enum NfcHelperState { Initializing, Waiting, Ready, Writing, Fini

我试图允许人们使用我的应用程序写入NFC标签,这样我的应用程序就可以使用自定义参数启动。我想能够重新编程的NFC标签上已经有数据

我正在使用下面的代码,但问题是,WP总是识别NFC标记上已经存在的操作并中断,因为它希望启动之前任何时候编写的NFC标记操作

我如何告诉操作系统停止触发标记的操作,以便我可以立即重写它

public enum NfcHelperState
{
    Initializing,
    Waiting,
    Ready,
    Writing,
    Finished,
    Error,
    NoDeviceFound
}
public class NfcHelper
{
    private NfcHelperState _state = NfcHelperState.Initializing;

    public NfcHelperState State
    {
        get { return _state; }
    }


    private ProximityDevice _nfcDevice;

    private long _subscriptionId;
    public NfcHelper()
    {
        Init();
    }

    public void Init()
    {
        UpdateState();
        _nfcDevice = ProximityDevice.GetDefault();
        if (_nfcDevice == null)
        {
            UpdateState(NfcHelperState.NoDeviceFound);
            return;
        }
        UpdateState(NfcHelperState.Waiting);
    }

    private void UpdateState(NfcHelperState? state = null)
    {
        if (state.HasValue)
        {
            _state = state.Value;
        }
        if (OnStatusMessageChanged != null)
        {
            OnStatusMessageChanged(this, _state);
        }
    }

    public void WriteToTag()
    {
        UpdateState(NfcHelperState.Ready);
        _subscriptionId = _nfcDevice.SubscribeForMessage("WriteableTag", WriteableTagDetected);
    }

    private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message)
    {
        UpdateState(NfcHelperState.Writing);
        try
        {
            var str = "action=my_custom_action";
            str += "\tWindowsPhone\t";
            str += CurrentApp.AppId;
            _nfcDevice.PublishBinaryMessage("LaunchApp:WriteTag", GetBufferFromString(str),
                WriteToTagComplete);
        }
        catch (Exception e)
        {
            UpdateState(NfcHelperState.Error);
            StopWaitingForTag();
        }
    }

    private void WriteToTagComplete(ProximityDevice sender, long messageId)
    {
        sender.StopPublishingMessage(messageId);
        UpdateState(NfcHelperState.Finished);
        StopWaitingForTag();
    }

    private void StopWaitingForTag()
    {
        _nfcDevice.StopSubscribingForMessage(_subscriptionId);
    }

    private static IBuffer GetBufferFromString(string str)
    {
        using (var dw = new DataWriter())
        {
            dw.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
            dw.WriteString(str);
            return dw.DetachBuffer();
        }
    }

    public delegate void NfcStatusMessageChangedHandler(object myObject, NfcHelperState newState);

    public event NfcStatusMessageChangedHandler OnStatusMessageChanged;
}
当点击我的应用程序中的按钮,应用程序等待可写标记时,将调用
WriteToTag
。如果识别出可写标记,
WriteableTagDetected
将被调用并立即开始写入过程。但是,这会被询问是否执行NFC操作的WP对话框中断。写入后,应调用
WriteToTagComplete
,其中调用
StopWaitingForTag
并结束写入过程


我希望你们能帮我:)

结果我想错了。我不需要等待标签到达就可以重写它。实际上,不需要执行
\u nfcDevice.SubscribeForMessage(“WriteableTag”,WriteableTagDetected)在写入之前。只要开始使用
PublishBinaryMessage
,它将在到达设备时写入标记

我的最终代码如下所示:

public enum NfcHelperState
{
    Initializing,
    Ready,
    WaitingForWriting,
    FinishedWriting,
    ErrorWriting,
    NoDeviceFound
}
public class NfcHelper
{
    private NfcHelperState _state = NfcHelperState.Initializing;

    public NfcHelperState State
    {
        get { return _state; }
    }


    private ProximityDevice _nfcDevice;
    private long? _writingMessageId;

    public NfcHelper()
    {
        Init();
    }

    public void Init()
    {
        UpdateState();
        _nfcDevice = ProximityDevice.GetDefault();
        if (_nfcDevice == null)
        {
            UpdateState(NfcHelperState.NoDeviceFound);
            return;
        }


        UpdateState(NfcHelperState.Ready);
    }

    private void UpdateState(NfcHelperState? state = null)
    {
        if (state.HasValue)
        {
            _state = state.Value;
        }
        if (OnStatusMessageChanged != null)
        {
            OnStatusMessageChanged(this, _state);
        }
    }

    public void WriteToTag()
    {
        StopWritingMessage();
        UpdateState(NfcHelperState.WaitingForWriting);
        try
        {
            var str = new StringBuilder();
            str.Append("action=my_custom_action");
            str.Append("\tWindowsPhone\t{");
            str.Append(CurrentApp.AppId);
            str.Append("}");
            _writingMessageId = _nfcDevice.PublishBinaryMessage("LaunchApp:WriteTag", GetBufferFromString(str.ToString()),
                WriteToTagComplete);
        }
        catch
        {
            UpdateState(NfcHelperState.ErrorWriting);
            StopWritingMessage();
        }
    }

    private void WriteToTagComplete(ProximityDevice sender, long messageId)
    {
        UpdateState(NfcHelperState.FinishedWriting);
        StopWritingMessage();
    }

    private void StopWritingMessage()
    {
        if (_writingMessageId.HasValue)
        {
            _nfcDevice.StopPublishingMessage(_writingMessageId.Value);
            _writingMessageId = null;
        }
    }

    private static IBuffer GetBufferFromString(string str)
    {
        using (var dw = new DataWriter())
        {
            dw.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
            dw.WriteString(str);
            return dw.DetachBuffer();
        }
    }

    public delegate void NfcStatusMessageChangedHandler(object myObject, NfcHelperState newState);

    public event NfcStatusMessageChangedHandler OnStatusMessageChanged;
}