Windows phone 8 Windows Phone 8上的“后退”按钮

Windows phone 8 Windows Phone 8上的“后退”按钮,windows-phone-8,Windows Phone 8,我无法让back hardware按钮执行我希望它为Windows Phone 8执行的操作。该应用程序严格来说只是webview,所以现在当点击后退(硬件)按钮时,它会关闭该应用程序。我怎样才能解决这个问题,使它转到上一个网页或返回到索引或这些行上的东西 谢谢 这是我当前在MainPage.xaml.cs文件中的内容 namespace AvoidDiabetes { public partial class MainPage : PhoneApplicationPage { // U

我无法让back hardware按钮执行我希望它为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;
    }

}
名称空间避免糖尿病
{
公共部分类主页:PhoneApplicationPage
{
//主页的Url
私有字符串MainUri=“/Html/index.Html”;
私有堆栈_history=新堆栈();
私有Uri _current=null;
//建造师
公共主页()
{
初始化组件();
}
已加载专用无效浏览器(对象发送器、路由目标)
{
//在此处添加您的URL
浏览(新Uri(MainUri,UriKind.Relative));
Browser.isscript enabled=true;
}
//导航回web浏览器的导航堆栈,而不是应用程序。
私有void BackApplicationBar\u单击(对象发送方,事件参数e)
{
GoBack();
}
//在web浏览器的导航堆栈中向前导航,而不是在应用程序中向前导航。
private void ForwardApplicationBar\u单击(对象发送方,事件参数e)
{
GoForward();
}
//导航到初始“主页”。
私有void HomeMenuItem\u单击(对象发送者,事件参数e)
{
浏览(新Uri(MainUri,UriKind.Relative));
}
//处理导航故障。
私有无效浏览器\u导航失败(对象发送方,System.Windows.Navigation.NavigationFailedEventArgs e)
{
Show(“导航到此页面失败,请检查您的internet连接”);
}
BackKeyPress上受保护的覆盖无效(CancelEventArgs e)
{
基础。按Backkeypress(e);
如果(_history.Count==0)
{
//无历史记录,允许使用“后退”按钮
//或者做任何你需要做的事情,比如浏览应用程序页面
返回;
}
//否则,如果这不是第一次导航,请按当前
其他的
{
GoBack();
}
}
私有异步void WebBrowser_已导航(对象发送方,NavigationEventArgs e)
{
//如果我们返回,弹出最后一个条目
如果(_history.Count>0&&_history.Peek()==e.Uri)
{
_Pop();
}
//否则,如果这不是第一次导航,请按当前
否则如果(_current!=null)
{
_历史。推送(当前);
}
//当前页面现在是我们导航到的页面
_current=e.Uri;
}
}
}

您需要覆盖应用程序页面中的控件,并处理将控件导航回上一页的操作

假设您使用的是C#,下面是大致的操作方法(连接WebBrowser_导航到WebBrowser的导航事件):

private Stack_history=new Stack();
私有Uri _current=null;
BackKeyPress上受保护的覆盖无效(CancelEventArgs e)
{
基础。按Backkeypress(e);
如果(_history.Count==0)
{
//无历史记录,允许使用“后退”按钮
//或者做任何你需要做的事情,比如浏览应用程序页面
返回;
}
//取消按后退按钮
e、 取消=真;
//导航到最后一页
浏览(_history.Peek());
}
私有异步void WebBrowser_已导航(对象发送方,NavigationEventArgs e)
{
//如果我们返回,弹出最后一个条目
如果(_history.Count>0&&_history.Peek()==e.Uri)
{
_Pop();
}
//否则,如果这不是第一次导航,请按当前
否则如果(_current!=null)
{
_历史。推送(当前);
}
//当前页面现在是我们导航到的页面
_current=e.Uri;
}
这对我很有效, 您可以检查browser.CanGoBack()是否 如果是真的,返回,否则返回

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


        if (browser.CanGoBack)
        {
            e.Cancel = true;
            browser.GoBack();
        }
        else {
            return;
        }
    }

令人惊叹的。明天我会在我的monogame应用程序中尝试这个,然后再联系你!谢谢工作得很好。非常感谢,但我很抱歉,我对编程相当陌生,有点迷茫。当我将其添加到我的web浏览器的导航页面时,CancelEventArgs事件被下划线为错误时,我遇到了问题。有什么建议吗?您是否在源文件中“使用System.ComponentModel;”?在文件顶部,您应该看到其他使用语句。如果缺少一个,您需要添加它。它通知编译器搜索该命名空间以解析CancelEventArgs类型。
   protected override void OnBackKeyPress(CancelEventArgs e) {
        base.OnBackKeyPress(e);


        if (browser.CanGoBack)
        {
            e.Cancel = true;
            browser.GoBack();
        }
        else {
            return;
        }
    }