c# - 是否可以从 webbrowser NavigationFailed 事件中导航?

标签 c# windows-phone-8 windows-phone

private void webBrowser_NavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        Debug.WriteLine("Navigation Failed");

        if (!Utils.IsNetworkAvailable())
        {
            MessageBoxResult result = MessageBox.Show("Please go to Settings and enable your network connection.",
                                                 "No network connection", MessageBoxButton.OK);

            if (result == MessageBoxResult.OK)
            {                    
                Dispatcher.BeginInvoke(() => 
                    NavigationService.Navigate(new Uri("/TutorialPage.xaml", UriKind.Relative))); //TODO: Doesnt work
            }
        }
}

这可能吗?我想转到上一个 xaml 页面而不是网页。

提前谢谢你。

最佳答案

我试过你的代码,运行正常。 Here is my simple example .您可以在 NavigationFailedEvent 中导航 - 问题是您永远无法到达那里。

正如我所尝试的,问题主要与模拟器有关——可能是由于互联网连接是如何实现的。例如:

web.Navigate(new Uri(@"http://nosite.azd", UriKind.Absolute));

此导航在我的模拟器上没有失败(我被重定向到了某个地方),但正如我在设备上测试的那样 - 它失败了。

因此请尝试在设备上测试您的应用。但 IMO 最好在导航(加载 Web 浏览器)之前检查互联网连接,而不是等待导航失败(这可能是额外的检查)。

此外,您不需要通过 Dispatcher 进行导航,因为您的代码在主线程上运行。

关于c# - 是否可以从 webbrowser NavigationFailed 事件中导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792531/

相关文章:

c# - 在 C# 中发送 http 请求并捕获网络问题

windows-phone-8 - 如何向测试人员分发 WP8 应用程序?

c# - Windows Phone - 使用互斥锁进行独立存储

c# - 诺基亚视频修剪器和 Moliplayer 如何访问视频?

c# - 如何使用 PInvoke 从 C# 导入 C++ dll?

c# - 不带 "run as administrator"的导出事件日志 (.evtx)

c# - 牛顿软件.JSON : JSON List and Array are handled differently

c# - Windows Phone 使 Segoe UI 符号不显示为表情符号

c# - Windows Phone 8.1 滚动查看器不滚动

android - Cocos2d-x跨平台游戏开发