过早使用 NavigationService.GoBack() 时 Windows Phone 应用程序崩溃

发布于 2024-12-08 18:02:36 字数 1532 浏览 0 评论 0原文

即使 NavigationService.CanGoBack 返回 TrueNavigationService.GoBack() 也会引发这些异常:

A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in 

这会在两种情况下系统地发生,而第三个工作正常:

  • 如果我调用 NavigationService.GoBack() 就会崩溃 OnNavieratedTo()
  • 如果我调用就会崩溃NavigationService.GoBack() 由于 Internet 不可用时在我的 HTTPWebRequest 中抛出 WebException [1]
  • 如果互联网可用,并且当我的 HTTPWebRequest 获取结果、解析并显示结果时,我会调用 NavigationService.GoBack() ,则工作正常。

我的理论是,从一个页面导航到另一个页面后,我不能太快调用 GoBack()...我的问题:当 < code>HTTPWebRequest 无法加载?

编辑:我决定采用另一种方式,但我认为我的问题可能是由于导航动画和 Windows Phone C# 工具包(我使用 2011 年 2 月版) )


[1] 详情我的案例 2 代码:

我有一个简单的 HTTPWebRequest。我的回调执行此操作,并且我的应用程序在飞行模式下崩溃。尽管 NavigationService.CanGoBack 返回 true,但 NavigationService.GoBack() 行负责。

        try
        {
            response = request.EndGetResponse(result);
        }
        catch (WebException)
        {
            Dispatcher.BeginInvoke(() =>
            {
                NavigationService.GoBack();
            });
        }

我也尝试使用 Deployment.Current.Dispatcher.BeginInvoke() 。

Even though NavigationService.CanGoBack returns True, NavigationService.GoBack() throws me these exceptions :

A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in 

This happens systematically on two case, while the third works fine :

  • Crashes if I call NavigationService.GoBack() in OnNavigatedTo()
  • Crashes If I call NavigationService.GoBack() as a result of WebException thrown in my HTTPWebRequest when Internet is not available [1]
  • Works fine if Internet is available and I call NavigationService.GoBack() when my HTTPWebRequest got results, parsed them, and displayed them.

My theory is that I can't call GoBack() too soon after navigating from a page to another... My question : How can I programatically go back up the navigation stack when an HTTPWebRequest fails to load ?

Edit : I've decided to do it another way, but I think my problems might be due to navigation animations and the Windows Phone C# Toolkit (I use Feb 2011 edition)


[1] Details of my code on case 2 :

I have a simple HTTPWebRequest. My callback does this, and my app crashes when in Airplane Mode. The line NavigationService.GoBack() is responsible, even though NavigationService.CanGoBack returns true.

        try
        {
            response = request.EndGetResponse(result);
        }
        catch (WebException)
        {
            Dispatcher.BeginInvoke(() =>
            {
                NavigationService.GoBack();
            });
        }

I tried using Deployment.Current.Dispatcher.BeginInvoke() also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-12-15 18:02:36

您可以尝试使用 WebClient client = new WebClient();,然后使用 client.DownloadStringAsync(new Uri("request_url")); 发出请求并订阅client.DownloadStringCompleted 事件,用于在请求完成时接收您的数据。解析数据后,您可以在事件处理程序中调用 NavigationService.GoBack(); 或转到您想要的任何页面。

另外,如果您尝试在 OnNavigedTo 事件中执行某些操作并遇到麻烦,您可以尝试使用 OnNavigatingFrom (在 c 的上一页),取消导航 e.Cancel = true;,按照发出请求等操作,然后获取应用程序框架并导航到 e.Uri (基本上继续您之前取消的导航) 。

尽管第二个也可能代表一个解决方案,但我认为第一个更好,因为它异步完成所有工作,因此不会阻塞您的 UI 线程。这是我通常在我的应用程序中使用的。希望有帮助。

You could try using WebClient client = new WebClient();, then use client.DownloadStringAsync(new Uri("request_url")); to make your request and subscribe to the client.DownloadStringCompleted event to receive your data when the request is completed. After parsing the data, in the event handler you can then call NavigationService.GoBack(); or go to whichever page you want.

Also, if you try to do something in the OnNavigatedTo event and run into trouble you could try using the OnNavigatingFrom instead (on the previous page ofc), cancel the navigation e.Cancel = true;, do your thing as in make the request and stuff, then get the application frame and navigate to e.Uri (basically continuing the navigation you previously cancelled).

Altho this second might represent a solution as well, I think the first one is better as it does all the work async thus not blocking your UI thread. This is what I generally use in my apps. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文