简体   繁体   English

在Mvx WP8应用程序中使用NFC

[英]Using NFC with Mvx WP8 Application

I am attempting to use NFC in an Windows Phone 8 application, which uses the MvvmCross framework. 我试图在使用MvvmCross框架的Windows Phone 8应用程序中使用NFC。 Now normally you subscribe to receive NFC events on WP8 by adding an Extension to the WMAppManifest.xml like so: 现在,通常您可以通过向WMAppManifest.xml添加Extension来订阅WP8上的NFC事件,如下所示:

<Extensions>
    <Protocol Name="my-resource" NavUriFragment="encodedLaunchUri=%s" TaskId="_default" />
</Extensions>

This will launch the _default task, if it finds a uri starting with my-resource:// , which on a fresh project is the MainPage.xaml . 如果找到以my-resource://开头的uri,则它将启动_default任务,在新项目中为MainPage.xaml In this case I have set it to Views\\ScanView.axml , which is a MvxPhonePage . 在这种情况下,我将其设置为Views\\ScanView.axml ,这是一个MvxPhonePage

Then to get the data in the _default task, you would override OnNavigatedTo and grab the e.Uri , which is the data from the NFC tag. 然后,要在_default任务中获取数据,您将覆盖OnNavigatedTo并获取e.Uri ,这是NFC标签中的数据。 Ie: /Protocol?encodedLaunchUri=my-resource://ni?EkkeEkkeEkkeEkkePtangyaZiiinngggggggNi . 即: /Protocol?encodedLaunchUri=my-resource://ni?EkkeEkkeEkkeEkkePtangyaZiiinngggggggNi

Now it seems that the MvxPhonePage overrides OnNavigatedTo on its own and uses it for some save states. 现在看来, MvxPhonePage会自己覆盖OnNavigatedTo并将其用于某些保存状态。 So my question is. 所以我的问题是。 How do I get original Uri instead of the saved state? 如何获取原始Uri而不是保存状态?

Can I just work around it by using the MainPage.axml and then when I am done loading the NFC stuff navigate to Views\\ScanView.axml ? 我可以使用MainPage.axml来解决该问题,然后在完成NFC加载后导航到Views\\ScanView.axml吗?

I solved the problem by creating a custom AppStart which is briefly described in this Slide Deck , which Stuart Lodge told me to look at. 我通过创建一个自定义AppStart解决了该问题,在本Slide Deck中对此进行了简要介绍,Stuart Lodge告诉我进行了介绍。

So in my ScanViewModel I added an Init(string url) method, which handles navigation with extra parameters, in this case my wanted Url, and then I can handle it as I want there. 因此,在我的ScanViewModel我添加了一个Init(string url)方法,该方法处理带有额外参数的导航,在这种情况下,是我想要的Url,然后我就可以在那里处理它。

In App.xaml.cs where you normally call the Start() method of the AppStart I added some conditions: 在通常调用AppStartStart()方法的App.xaml.cs ,我添加了一些条件:

var start = Mvx.Resolve<IMvxAppStart>();
var url = navigatingCancelEventArgs.Uri.ToString();
if(url.StartsWith(@"/Protocol?encodedLaunchUri=my-resource")
    start.Start(url.SubString("/Protocol?encodedLaunchUri=".Length));
else
    start.Start();

Then I had to create my own AppStart : 然后,我必须创建自己的AppStart

public class MyCustomAppStart : MvxNavigatingObject, IMvxAppStart
{
    public void Start(object hint = null)
    {
        if(hint is string)
            ShowViewModel<ScanViewModel>(new {url = (string)hint});
        else
            ShowViewModel<ScanViewModel>();
    }
}

Which I instantiate in the MvxApplcation Initialize method: 我在MvxApplcation Initialize方法中实例化的:

RegisterAppStart(new MyCustomAppStart());

Then I get the desired Url in Init in the ViewModel: 然后在ViewModel的Init中获得所需的Url:

public void Init(string url)
{
    //Whatever I want, whatever I need
}

Another alternative is to define a custom UriMapper to catch and validate the external launch URI, and then return the URI of the actual page you want to launch. 另一种选择是定义一个自定义UriMapper来捕获并验证外部启动URI,然后返回要启动的实际页面的URI。 That way you keep the external launch logic away from your page navigation logic. 这样,您可以使外部启动逻辑远离页面导航逻辑。

See UriMapperBase docs for the basics. 有关基础知识,请参见UriMapperBase文档。 You'll need to add the UriMapper to your PhoneApplicationFrame at the correct point. 您需要在正确的位置将UriMapper添加到PhoneApplicationFrame中。

In App.xaml.cs: 在App.xaml.cs中:

// Do not add any additional code to this method
// ;)
private void InitializePhoneApplication()
{
    if (phoneApplicationInitialized)
        return;

    // Create the frame but don't set it as RootVisual yet; this allows the splash
    // screen to remain active until the application is ready to render.
    RootFrame = new PhoneApplicationFrame();

    // TODO: Add custom UriMapper here
    RootFrame.UriMapper = new MyCustomUriMapper();

    RootFrame.Navigated += CompleteInitializePhoneApplication;

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM