Reputation: 121
I am working on a windows phone 8.1 universal app. I am pulling a feed and then displaying it in a listbox. Each item in the feed takes you to a web page. 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. 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. 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?
here is my 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:
private void SearchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox listBox = sender as 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:
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!
Upvotes: 0
Views: 449
Reputation: 21889
How are you currently handling the HardwareButton.BackPressed event?
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.
If you're using the NavigationHelper functions you can override the GoBack command to do this.
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. The Frame.GoBack call will go back, so if you don't want to navigate then don't call it.
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;
}
}
Upvotes: 0