[英]Using NFC with Mvx WP8 Application
我试图在使用MvvmCross框架的Windows Phone 8应用程序中使用NFC。 现在,通常您可以通过向WMAppManifest.xml
添加Extension
来订阅WP8上的NFC事件,如下所示:
<Extensions>
<Protocol Name="my-resource" NavUriFragment="encodedLaunchUri=%s" TaskId="_default" />
</Extensions>
如果找到以my-resource://
开头的uri,则它将启动_default
任务,在新项目中为MainPage.xaml
。 在这种情况下,我将其设置为Views\\ScanView.axml
,这是一个MvxPhonePage
。
然后,要在_default
任务中获取数据,您将覆盖OnNavigatedTo
并获取e.Uri
,这是NFC标签中的数据。 即: /Protocol?encodedLaunchUri=my-resource://ni?EkkeEkkeEkkeEkkePtangyaZiiinngggggggNi
。
现在看来, MvxPhonePage
会自己覆盖OnNavigatedTo
并将其用于某些保存状态。 所以我的问题是。 如何获取原始Uri而不是保存状态?
我可以使用MainPage.axml
来解决该问题,然后在完成NFC加载后导航到Views\\ScanView.axml
吗?
我通过创建一个自定义AppStart
解决了该问题,在本Slide Deck中对此进行了简要介绍,Stuart Lodge告诉我进行了介绍。
因此,在我的ScanViewModel
我添加了一个Init(string url)
方法,该方法处理带有额外参数的导航,在这种情况下,是我想要的Url,然后我就可以在那里处理它。
在通常调用AppStart
的Start()
方法的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();
然后,我必须创建自己的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>();
}
}
我在MvxApplcation
Initialize
方法中实例化的:
RegisterAppStart(new MyCustomAppStart());
然后在ViewModel的Init
中获得所需的Url:
public void Init(string url)
{
//Whatever I want, whatever I need
}
另一种选择是定义一个自定义UriMapper
来捕获并验证外部启动URI,然后返回要启动的实际页面的URI。 这样,您可以使外部启动逻辑远离页面导航逻辑。
有关基础知识,请参见UriMapperBase文档。 您需要在正确的位置将UriMapper
添加到PhoneApplicationFrame
中。
在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.