简体   繁体   English

使用Windows Phone 8.1中的webview导航回列表框视图页面

[英]navigate back to the listbox view page using webview in windows phone 8.1

I am working on a windows phone 8.1 universal app. 我正在开发Windows Phone 8.1通用应用程序。 I am pulling a feed and then displaying it in a listbox. 我正在提取一个Feed,然后将其显示在列表框中。 Each item in the feed takes you to a web page. Feed中的每个项目都会将您带到一个网页。 I am using WebView control to show the content of the web page when someone clicks one of the items in the listbox view page. 当有人单击列表框视图页面中的某个项目时,我正在使用WebView控件来显示网页的内容。 I can show the web page in the WebView control, but when I press the hardware back button, it takes me back to the mainpage(where I started from) instead of the listbox view page. 我可以在WebView控件中显示网页,但是当我按下硬件后退按钮时,它会将我带回主页(我从哪里开始)而不是列表框视图页面。 How can I go back to the listbox view page so the user can click on yet another item to view that in the WebView control? 如何返回到列表框视图页面,以便用户可以单击另一个项目以在WebView控件中查看该项目?

here is my XAML: 这是我的XAML:

<WebView Grid.Row="0" Name="webBrowser1" Visibility="Collapsed"  Width="auto" Height="auto" Grid.RowSpan="2" NavigationCompleted="Mywebbrowser_LoadCompleted"/>

Here is my selection changed code on the listbox view page that takes the user to the web page in the webview control: 以下是我在列表框视图页面上选择更改的代码,该代码将用户带到webview控件中的网页:

private void SearchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox listBox = sender as ListBox; private void SearchListBox_SelectionChanged(object sender,SelectionChangedEventArgs e){ListBox listBox = sender作为ListBox;

  if (listBox != null && listBox.SelectedItem != null)
  {
      // Get the item that was tapped.
      SearchListItem sItem = (SearchListItem)listBox.SelectedItem;

      // Set up the page navigation only if a link actually exists in the feed item.
      if (sItem.Url.Length > 0)
      {
          // Get the associated URI of the feed item.
          Uri site = new Uri(sItem.Url.Replace("https", "http"));

          //Set up the app bar once the feed items are displayed in the web browser control 
          //appbar();

          // Show the progress bar.....
          mycontrols.progressbarShow(pgbar, pgText);

          //appbar_eh.appbarNoShow(ApplicationBar);
          //mycontrols_eh.progressbarShow(pgbar, pgText);
          webBrowser1.Visibility = Windows.UI.Xaml.Visibility.Visible;
          webBrowser1.Source = site;
      }
   }
}

Edit added BackPressed handler: 编辑添加的BackPressed处理程序:

void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) 
{
    Frame frame = Window.Current.Content as Frame; 
    if (frame == null) 
    {
        return; 
    } 
    if (frame.CanGoBack) 
    { 
        frame.GoBack(); 
        e.Handled = true; 
        webBrowser1.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
    } 
}

Thanks! 谢谢!

How are you currently handling the HardwareButton.BackPressed event? 你目前如何处理HardwareButton.BackPressed事件?

Since it goes back to your mainpage rather than exiting the app you must have some handler for it (either directly or via library code). 由于它返回到您的主页而不是退出应用程序,您必须有一些处理程序(直接或通过库代码)。 You'll need to modify that code to hide your WebView if it's open instead of performing its normal navigation. 如果WebView处于打开状态而不是执行正常导航,则需要修改该代码以隐藏它。

If you're using the NavigationHelper functions you can override the GoBack command to do this. 如果您正在使用NavigationHelper函数,则可以覆盖GoBack命令来执行此操作。

Edit: Based on your added sample code, here's one possible way. 编辑:根据您添加的示例代码,这是一种可能的方式。 The basic idea is to close the WebView instead of calling Frame.GoBack rather than doing both. 基本思想是关闭WebView而不是调用Frame.GoBack而不是两者兼而有之。 The Frame.GoBack call will go back, so if you don't want to navigate then don't call it. Frame.GoBack调用将返回,因此如果您不想导航,请不要调用它。

void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) 
{
    Frame frame = Window.Current.Content as Frame; 
    if (frame == null) 
    {
        return; 
    } 
    // Depending on the app there may be higher level state than 
    // directly checking the Visibility property 
    if (webBrowser1.Visibility == Windows.UI.Xaml.Visibility.Visible)
    {
        // If webBrowser is open then close it rather than
        // navigating to the previous page
        webBrowser1.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
        e.Handled = true; 
    }
    else if (frame.CanGoBack) 
    { 
        frame.GoBack(); 
        e.Handled = true; 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM