locked
How to get the CanGoBack property and GoBack() method for windows 8 store app

    Question

  • Hi Everybody,

            I m working windows 8 store app with webview. Here I have two buttons for go back and go forward. But i didn't have default  property for goback and goforward. How can i achieve this? 

    Tuesday, December 30, 2014 6:25 AM

Answers

  • Thanks Rob Caplan

    Actually I want solution for XAML.

    I achieve this by using list and maintaining the the current item position of current URL.

    See the Sample code:

    In webview loadingcomplete event track the url navigations

    private int _bacListPositionElevate = -1;
    
     webView.LoadCompleted += (object s, NavigationEventArgs e) =>
                {
                    _currentUri = e.Uri;
                    if (_currentUri != null)
                    {
                            if (_currentUri != elevateNavigationStack[_bacListPositionElevate])
                            {
                                elevateNavigationStack.Add(_currentUri);
                                _bacListPositionElevate++;
                            }
                    }
                };

    Now in the back button decrease the position like this:

     _bacListPositionElevate--;
                webView.Navigate(elevateNavigationStack[_bacListPositionElevate]);
                BackButton.IsTapEnabled = (_backStackPositionElevate != 0);
                FrwdButton.IsTapEnabled = (_backStackPositionElevate < elevateNavigationStack.Count - 1);

    Now in the forward button increase the position like this:

     _bacListPositionElevate++;
                wvElevate.Navigate(elevateNavigationStack[_bacListPositionElevate]);
                imgElevateBack.IsTapEnabled = (_backStackPositionElevate != 0);
                imgElevateFwrd.IsTapEnabled = (_backStackPositionElevate < elevateNavigationStack.Count - 1);

    • Marked as answer by Shaafs Tuesday, January 6, 2015 7:42 AM
    Tuesday, January 6, 2015 7:31 AM

All replies

  • Navigation functions such as GoBack and GoForward are new for the WebView in Windows 8.1. They are not available for Windows 8 apps.

    To navigate a WebView in Windows 8 you'll need to inject JavaScript code into the WebView to perform the navigation, but I'd recommend upgrading to Windows 8.1.

    Tuesday, December 30, 2014 6:36 AM
    Owner
  • Thanks for your response.

    I want for windows 8 only.

    Tuesday, December 30, 2014 6:41 AM
  • Then you'll have to inject JavaScript and navigate within the HTML's DOM. The Windows 8 WebView doesn't have any support for navigation.

    Matt's blog entry Ten Things You Need to Know About WebView includes notes on how to inject JavaScript into a WebView.

    Tuesday, December 30, 2014 7:12 PM
    Owner
  • Thanks Rob Caplan

    Actually I want solution for XAML.

    I achieve this by using list and maintaining the the current item position of current URL.

    See the Sample code:

    In webview loadingcomplete event track the url navigations

    private int _bacListPositionElevate = -1;
    
     webView.LoadCompleted += (object s, NavigationEventArgs e) =>
                {
                    _currentUri = e.Uri;
                    if (_currentUri != null)
                    {
                            if (_currentUri != elevateNavigationStack[_bacListPositionElevate])
                            {
                                elevateNavigationStack.Add(_currentUri);
                                _bacListPositionElevate++;
                            }
                    }
                };

    Now in the back button decrease the position like this:

     _bacListPositionElevate--;
                webView.Navigate(elevateNavigationStack[_bacListPositionElevate]);
                BackButton.IsTapEnabled = (_backStackPositionElevate != 0);
                FrwdButton.IsTapEnabled = (_backStackPositionElevate < elevateNavigationStack.Count - 1);

    Now in the forward button increase the position like this:

     _bacListPositionElevate++;
                wvElevate.Navigate(elevateNavigationStack[_bacListPositionElevate]);
                imgElevateBack.IsTapEnabled = (_backStackPositionElevate != 0);
                imgElevateFwrd.IsTapEnabled = (_backStackPositionElevate < elevateNavigationStack.Count - 1);

    • Marked as answer by Shaafs Tuesday, January 6, 2015 7:42 AM
    Tuesday, January 6, 2015 7:31 AM