locked
navigate back to the listbox view page using webview control in windows phone 8.1 RRS feed

  • Question

  • 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;
                    }
    
                }

    thanks!


    mujno

    Tuesday, February 17, 2015 2:18 AM

Answers

  • maybe an example could help you better

    Page.xaml
    <Page 
        ....
        Loaded="OnPageLoaded">

    Page.xaml.cs

    void LoginPage_Loaded(object sender, RoutedEventArgs e)
            {
    #if WINDOWS_PHONE_APP
                Windows.Phone.UI.Input.HardwareButtons.BackPressed+=HardwareButtons_BackPressed;
    #endif
            }
    
    void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        e.Handled = true;//i'm not 100% sure that this is how we cancel it but it's the only property BackPressdEventArgs has
        webBrowser1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        //maybe you need a story board to make it *fade out*
    }

    Tuesday, February 17, 2015 6:55 AM
  • sorry I recently set my email to block all strangers so the msdn newsletters couldn't get through

    about fade out animation. I recommend you to make it in blend. it's quite easy just like using office and dose not even require coding experience.

    also, I asked a similar question few days ago. you can get your answer there

    https://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/f8d607c2-8e7f-4aa2-9787-5aa693d1d6f6/

    Saturday, February 21, 2015 4:33 PM

All replies

  • it would be quicker and easier for you just by doing a little bit of searching

    here is the answer from another thread https://social.msdn.microsoft.com/Forums/en-US/8ea46f7d-175a-427a-93ae-350f7a10a3d3/can-i-stop-hardware-back-button?forum=wpdevelop

    void OnBackKeyPressed( object sender, CancelEventArgs e )
    {
      var result = MessageBox.Show( "Do you want to exit?", "Attention!", 
                                    MessageBoxButton.OKCancel );
    
      if( result == MessageBoxResult.OK ) {
        // Do not cancel navigation
        return;
      }
      e.Cancel = true;
    }
    
    Use the code to disable back key to navigation.
    
    

    Tuesday, February 17, 2015 2:29 AM
  • Thanks for the reply. So I am not looking to disable the back hardware key, I am looking for help to go back to the listbox page and not to the page where I started.

    Also, the code snippet you gave me doesn't work in Windows Phone universal app I am developing. When I copied and pasted your snippet in my code, on pressing the back key, the function never got executed when I debugged. Do you know how to change the snippet for a Windows phone 8.1 universal app?

      


    mujno

    Tuesday, February 17, 2015 6:02 AM
  • Of cause you're not going to disable the back button, i understand that.
    but before you cancel the default navigate back behavior you have the chance to
    control your webview and make it to do the correct thing. As for not getting
    the method executed, i believe you should subscribe to the event first.
    However… it's true that that snippet is not working because OnBackKeyPressed
    event does not seems to exist. instead I found
    Windows.Phone.UI.Input.HardwareButtons.BackPressed event (just type in Windows.Phone.Ui.Input since such event is most likely to be in that namespace and i found it through intellisence). Subscribe to that
    event somewhere like the constructor of App or the OnPageLoaded method. But not the OnNavigatedTo
    method because you don't want to subscribe it multiple times when the user navigates back and forth between pages.

    you will also need to use #if WINDOWS_PHONE_APP ... #endif since that event is only available on windows phone

    Tuesday, February 17, 2015 6:43 AM
  • maybe an example could help you better

    Page.xaml
    <Page 
        ....
        Loaded="OnPageLoaded">

    Page.xaml.cs

    void LoginPage_Loaded(object sender, RoutedEventArgs e)
            {
    #if WINDOWS_PHONE_APP
                Windows.Phone.UI.Input.HardwareButtons.BackPressed+=HardwareButtons_BackPressed;
    #endif
            }
    
    void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        e.Handled = true;//i'm not 100% sure that this is how we cancel it but it's the only property BackPressdEventArgs has
        webBrowser1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        //maybe you need a story board to make it *fade out*
    }

    Tuesday, February 17, 2015 6:55 AM
  • Thanks for the sample code, that helped and got me a little further. So now the backkey press function does get executed but collapsing the webview doesn't really take me back to the listbox page. Have any code to make a story board to try to fade it out?

    mujno

    Wednesday, February 18, 2015 5:33 AM
  • sorry I recently set my email to block all strangers so the msdn newsletters couldn't get through

    about fade out animation. I recommend you to make it in blend. it's quite easy just like using office and dose not even require coding experience.

    also, I asked a similar question few days ago. you can get your answer there

    https://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/f8d607c2-8e7f-4aa2-9787-5aa693d1d6f6/

    Saturday, February 21, 2015 4:33 PM