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/