Mar*_*han 3 nfc windows-phone-8
我有Windows Phone 8设备,几个NDEF格式的NFC标签,我想知道,是否可以使用这些标签在我的WP8上实现应用程序启动?我已经仔细阅读了这篇关于为Windows Phone 8启动内置应用程序的URI方案的文章,但我没有找到任何与实际启动第三方应用程序相关的链接.我可以启动各种设置屏幕,或浏览器,电子邮件,短信...
更有趣的是,WP Store上至少有两个NFC标签可以"编写用于启动应用程序的标签",我已经尝试过了,但启动工作无效.
所以问题是:是否可以存储NFC标签信息,以便在WP8上启动任何第三方应用程序?如果是,这种URI方案的格式是什么以及如何使用WP8将其写入标签?
是的,您可以使用NFC标签启动任何Windows Phone 8应用程序.您需要在具有LaunchApp记录作为第一条记录的标记上放置NDEF消息.将NDEF记录的有效负载中的平台ID设置为"WindowsPhone",并将应用程序ID设置为"{"和"}"之间Windows Phone存储URL末尾的十六进制字符串.例如,对于http:// www. windowsphone.com/en-us/store/app/stackoverflow-feeds/226fcc72-69ff-4a85-b945-cbb7f5ea00af到"{226fcc72-69ff-4a85-b945-cbb7f5ea00af}".
该库可以帮助创建此类NDEF记录.MS提供的有限文档可在此处获得.
要通过NFC标签启动您的应用,您需要通过在WMAppManifest.xml文件中添加扩展名来注册URI关联,如下所示:
<Extensions>
  <Protocol Name="mynfcapp" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
然后,您需要创建一个可以处理URI关联的URI映射器,如下所示:
public class AssociationUriMapper : UriMapperBase
{
    public override Uri MapUri(Uri uri)
    {
        string url = HttpUtility.UrlDecode(uri.ToString());
        if (url.Contains("mynfcapp:MainPage"))
        {
            int paramIndex = url.IndexOf("source=") + 7;
            string paramValue = url.Substring(paramIndex);
            return new Uri("/MainPage.xaml?source=" + paramValue, UriKind.Relative);
        }
        return uri;
    }
}
以下是编写将启动应用程序的NFC标签的代码:
public partial class MainPage : PhoneApplicationPage
{
    private readonly ProximityDevice _proximityDevice;
    private long subId = 0;
    private long pubId = 0;
    public MainPage()
    {
        InitializeComponent();
        _proximityDevice = ProximityDevice.GetDefault();
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (_proximityDevice != null)
            subId = _proximityDevice.SubscribeForMessage("WriteableTag", OnWriteableTagArrived);
        base.OnNavigatedTo(e);
    }
    private void OnWriteableTagArrived(ProximityDevice sender, ProximityMessage message)
    {
        var dataWriter = new DataWriter();
        dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
        string appLauncher = string.Format(@"mynfcapp:MainPage?source=mynfctest");
        dataWriter.WriteString(appLauncher);
        pubId = sender.PublishBinaryMessage("WindowsUri:WriteTag", dataWriter.DetachBuffer());
    }
}