windows-phone-8 - Windows Phone 8 上的后退按钮

标签 windows-phone-8

我无法让后退硬件按钮执行我希望它为 Windows Phone 8 执行的操作。该应用程序严格来说只是 webview,因此到目前为止,当单击后退(硬件)按钮时,它会关闭该应用程序。我该如何解决这个问题,以便它转到上一个网页或返回到索引或这些行上的内容?

谢谢

这是我目前在 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;
    }

}

}

最佳答案

您需要覆盖 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;
}

关于windows-phone-8 - Windows Phone 8 上的后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14630765/

相关文章:

.net - Windows Phone 8.1 - 存储空间不足异常?

c# - 如何在不播放或存储的情况下获取 mp3 文件的持续时间

windows-phone-7 - 如何从 Windows Phone 7 或 8 获取用户自己的手机号码

c# - 无法连接到蓝牙打印机

html - Windows Phone 8 的 IE10 中的奇怪文本大小

javascript - window.open 在 Windows Phone 8 上返回 null

xaml - 水平拉伸(stretch) ItemsControl 中的内容

windows-phone-8 - 在 Windows Phone 8 中以编程方式发送短信

c# - 无法将类型 System.EventHandler 隐式转换为 System.EventHandler<object> 错误

web-services - 为什么 Windows Phone 应用程序的 REST 服务中会出现 NotFound 错误?