导航到ContinueFileOpenPicker方法不同的页面 [英] Navigate to a different page from ContinueFileOpenPicker method

查看:277
本文介绍了导航到ContinueFileOpenPicker方法不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是FilePicker的Windows应用程序的通用,我试图从MainPage.xaml中启动该文件选择器,然后导航到不同的页面(LoadPhoto.xaml)呈现选定的图像。

I'm using the FilePicker for Windows Universal apps and I'm trying to launch the File Picker from MainPage.xaml and then navigate to a different page (LoadPhoto.xaml) to render the selected image.

我初步实现了我的应用程序,这样我会浏览到LoadPhoto.xaml然后,作为加载页面的一部分,我会打电话的文件选择器。但是,这种恢复应用程序时造成的问题,让我感动的文件选择器调用出来的构造。

I initially implemented my app so that I'd navigate to LoadPhoto.xaml and then, as part of loading the page, I'd call the File Picker. However that caused issues when resuming the app, so I moved the File Picker call out of the constructor.

在最新的实施,我称之为从MainPage.xaml中的文件选择器然后,如果照片被选中,导航到LoadPhoto.xaml。然而,似乎是一个竞争条件的地方:有时,应用程序选择画面(它看起来像它的实际导航到LoadPhoto页面,但东西使得帧回去的MainPage)后保持MainPage执行。其他时候,应用程序成功地导航到LoadPhoto页面并呈现图像,但如果我导航回来的后退按钮,然后再按挑选照片按钮时,FilePicker短暂显示,然后应用程序崩溃。此行为不会VS连接摄制。而在调试模式下执行,一切工作正常。

In the newest implementation, I call the file picker from MainPage.xaml and then, if a photo has been selected, navigate to LoadPhoto.xaml. However, there seems to be a race condition somewhere: Sometimes, the app stays on the MainPage after selecting a picture (it looks like it's actually navigating to the LoadPhoto page but something makes the Frame go back to the MainPage). Other times, the app successfully navigates to the LoadPhoto page and renders the image, but if I navigate back with the Back button and then press the Pick photo button again, the FilePicker is briefly shown and then the app crashes. This behavior doesn't repro with VS attached. Everything works fine while executing in Debug mode.

我觉得根本原因是, ContinueFileOpenPicker 代码是从执行从该线程;工作线程,所以我应该不叫 this.Frame.Navigate(typeof运算(LoadPhoto),文件)。 ,这调用应该从主线程进行,但我不知道该怎么做。

I think the root cause is that the ContinueFileOpenPicker code is executed from a worker thread, so I shouldn't call this.Frame.Navigate(typeof(LoadPhoto), file); from that thread. That call should be made from the main thread, but I'm not sure how to do that.

不幸的是,这并没有解决这个问题:等待CoreWindow.GetForCurrentThread()Dispatcher.RunAsync(CoreDispatcherPriority.Norm人,()=> {this.Frame.Navigate(typeof运算(LoadPhoto),文件);});

Unfortunately, this doesn't fix the issue: await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Norm‌​al, () => { this.Frame.Navigate(typeof(LoadPhoto), file); });

我如何可以导航到不同的页面从ContinueFileOpenPicker方法?与摄制完整的代码这里

How can I navigate to a different page from the ContinueFileOpenPicker method? The full code with the repro is here.

推荐答案

当你打电话Frame.Navigate转到页LoadPhoto你逝去的一个复杂的对象作为参数:用户已选择了文件。当你回去炫魅广东,开始您的应用程序被暂停新的选择器会话,SuspensionManager序列化框架的状态(参见该类SaveFrameNavigationState方法)。不幸的是, GetNavigationState 在框架方法不支持序列化一个复杂的对象,而只是简单的像字符串,整数或GUID的。这是记录在MSDN上的Frame.Navigate方法。

When you call to Frame.Navigate to go to the LoadPhoto page you are passing a complex object as a parameter: the file the user has picked. When you go back to MainPage and start a new picker session your app gets suspended and SuspensionManager serializes the frame's state (see SaveFrameNavigationState method in that class). Unfortunately, the GetNavigationState method in Frame does not support serializing a complex object, but only simple ones like strings, integers or guids. This is documented in the Frame.Navigate method on MSDN.

你没有看到应用程序崩溃,当你在调试VS的原因是(默认)的应用程序不会被悬浮在这种情况下,让抛出异常的代码不会被调用。然而,没有调试连接,您的应用程序时,您导航远离它暂停。要强制停牌,使用的生命周期事件的下拉列表中的调试位置的工具栏上,一旦你已经启动了选择器会话。

The reason you don't see the app crashing when you are debugging in VS is because (by default) the app doesn't get suspended in this scenario, so the code that throws the exception is never called. However, with no debugger attached, your app is suspended when you navigate away from it. To force the suspension, use the Lifecycle Events drop-down in the Debug Location toolbar once you have launched the picker session.

如果你真的需要保存/恢复帧的状态,那么你应该避免航行时路过StorageFiles。您可以使用 FutureAccessList ,航行时的路径传递到文件并加载它LoadPhoto。

If you really need to save/restore the state of the frame, then you should avoid passing StorageFiles when navigating. You can use FutureAccessList, pass the path to the file when navigating and load it in LoadPhoto.

如果您不需要(或想要使用)什么SuspensionManager所提供的,那么你可以摆脱它,并保持传递StorageFile对象。但是,请记住,如果你这样做对该对象的引用将被保存在导航堆栈。

If you don't need (or want to use) what SuspensionManager has to offer, then you could get rid of it and keep passing a StorageFile object. However, keep in mind that if you do this a reference to that object will be kept in the navigation stack.

这篇关于导航到ContinueFileOpenPicker方法不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆