7

我无法让后退硬件按钮执行我希望它为 Windows Phone 8 执行的操作。该应用程序严格来说只是 web 视图,所以现在当单击后退(硬件)按钮时,它会关闭应用程序。我该如何解决这个问题,以便它转到上一个网页或返回到索引或那些行上的东西?

谢谢

这是我目前在 MainPage.xaml.cs 文件中的内容

namespace AvoidDiabetes
{
public partial class MainPage : PhoneApplicationPage
{
    // Url of Home page
    private string MainUri = "/Html/index.html";
    private Stack<Uri> _history = new Stack<Uri>();
    private Uri _current = null;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void Browser_Loaded(object sender, RoutedEventArgs e)
    {
        // Add your URL here
        Browser.Navigate(new Uri(MainUri, UriKind.Relative));
        Browser.IsScriptEnabled = true;
    }

    // Navigates back in the web browser's navigation stack, not the applications.
    private void BackApplicationBar_Click(object sender, EventArgs e)
    {
        Browser.GoBack();
    }

    // Navigates forward in the web browser's navigation stack, not the applications.
    private void ForwardApplicationBar_Click(object sender, EventArgs e)
    {
        Browser.GoForward();
    }

    // Navigates to the initial "home" page.
    private void HomeMenuItem_Click(object sender, EventArgs e)
    {
        Browser.Navigate(new Uri(MainUri, UriKind.Relative));


    }

    // Handle navigation failures.
    private void Browser_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
    {
        MessageBox.Show("Navigation to this page failed, check your internet connection");
    }



    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        base.OnBackKeyPress(e);


        if (_history.Count == 0)
        {
            // No history, allow the back button
            // Or do whatever you need to do, like navigate the application page
            return;
        }
        // Otherwise, if this isn't the first navigation, push the current
        else
        {
            Browser.GoBack();
        }




    }

    private async void WebBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        // If we navigated back, pop the last entry
        if (_history.Count > 0 && _history.Peek() == e.Uri)
        {
            _history.Pop();
        }
        // Otherwise, if this isn't the first navigation, push the current
        else if (_current != null)
        {
            _history.Push(_current);
        }

        // The current page is now the one we've navigated to
        _current = e.Uri;
    }

}

}

4

2 回答 2

11

您需要在应用程序的页面中覆盖OnBackKeyPress并处理将WebBrowser控件导航回上一页。

假设您使用的是 C#,大致可以这样做(将 WebBrowser_Navigated 连接到您的 WebBrowser 的 Navigated 事件):

private Stack<Uri> _history = new Stack<Uri>();
private Uri _current = null;

protected override void OnBackKeyPress(CancelEventArgs e)
{
    base.OnBackKeyPress(e);

    if (_history.Count == 0)
    {
        // No history, allow the back button
        // Or do whatever you need to do, like navigate the application page
        return;
    }

    // Cancel the back button press
    e.Cancel = true;

    // Navigate to the last page
    Browser.Navigate(_history.Peek());
}

private async void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
    // If we navigated back, pop the last entry
    if (_history.Count > 0 && _history.Peek() == e.Uri)
    {
        _history.Pop();
    }
    // Otherwise, if this isn't the first navigation, push the current
    else if (_current != null)
    {
        _history.Push(_current);
    }

    // The current page is now the one we've navigated to
    _current = e.Uri;
}
于 2013-02-11T22:18:41.110 回答
3

这对我有用,您可以检查 browser.CanGoBack() 是否为真,否则返回

   protected override void OnBackKeyPress(CancelEventArgs e) {
        base.OnBackKeyPress(e);


        if (browser.CanGoBack)
        {
            e.Cancel = true;
            browser.GoBack();
        }
        else {
            return;
        }
    }
于 2013-07-10T13:50:07.377 回答