微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,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,push the current
    else if (_current != null)
    {
        _history.Push(_current);
    }

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

原文地址:https://www.jb51.cc/windows/364592.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐