Windows Phone 8.1覆盖特定页面上的后退按钮 [英] Windows Phone 8.1 override back button on a certain page

查看:77
本文介绍了Windows Phone 8.1覆盖特定页面上的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发WP 8.1 XAML应用程序.第一页是登录页面,第二页例如是历史页面.将会有更多的页面,但是此刻并不重要.当我在登录页面上按登录按钮时,出现第二个(历史)页面.如果我按下历史记录页面上的(物理)后退按钮,它将返回到登录页面.但是我想禁用此功能.取而代之的是,当我按下后退"按钮时,会出现一个MessageDialog,如果我在几秒钟内(例如3秒钟)再次按下,则该应用程序将终止. 我试图找到问题的答案,但无法正常工作.我在登录页面上尝试过:

I am developing a WP 8.1 XAML app. The first page is a login page, the second for example a history page. There will be more pages, but it doesn't matter at the moment. When I press the login button on the login page, the second (history) page appears. If I press the (physical) back button on the history page, it goes back to the login page. But I would like to disable this function. Instead of this I would like when I press the back button a MessageDialog appears and if I press again within a few seconds (for example 3 secs) the app terminates. I tried to find the answer for my questions, but it doesn't work properly. I tried this on the login page:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    if (Frame.CurrentSourcePageType.FullName == "MyAppName.History")
        Application.Current.Exit();
}

当我按下第二页(历史记录)上的后退"按钮时,我的应用程序终止.但是,如果我尝试计算返回按钮的按下次数,它将导航回到登录页面.此后,当我再次登录并按返回按钮时,计数器将为2,应用程序将退出. 这是我尝试的代码:

My app terminates when I press the back button on the second (history) page. But if I try to count the pressing of back button, it will navigate back to the login page. After this when I login again and press the back button the counter will be 2 and the app will exit. Here is the code what I tried:

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    //MessageDialog msgbox = new MessageDialog("This is a popup message");
    //await msgbox.ShowAsync(); 
    //e.Handled = true;
    backPressCount++;

    if (backPressCount==2 && Frame.CurrentSourcePageType.FullName == "MyAppName.History")
        Application.Current.Exit();
}

如果我取消注释2条MessageDialog行,则会显示该消息,但我将导航回到登录页面. 而且我尝试取消注释e.Handled = true;行,但这没有帮助. 我认为我应该禁用历史记录页面上的后退"按钮或完全覆盖它. 有人知道我的问题吗?

If I uncomment the 2 MessageDialog lines, the message appears, but I will be navigated back to the login page. And I tried to uncomment the e.Handled = true; line, but it didn't help. I think I should disable the back button on history page or override it fully. Anybody has idea for my problem?

我重新考虑了后退按钮的行为,因为我不想使用代码来测量两次按下之间的时间.取而代之的是,我只想显示一个带有问题的弹出消息,例如您真的要退出吗?"和2个按钮(是和否).它可以与以下代码一起使用:

I rethink the back button behavivor, because I don't want to use ticker to measure the time between 2 press. Instead of this I would like only a popup message with a question, for example "Do you really want to quit?" and 2 buttons (Yes and No). It is works with the following code:

private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    e.Handled = true;
    MessageDialog dlg = new MessageDialog("Are you sure you want to quit you will loose all your work ?", "Warning");
    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(CommandHandler1)));
    dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(CommandHandler1)));

    await dlg.ShowAsync();
}

private void CommandHandler1(IUICommand command)
{
    var label = command.Label;
    switch (label)
    {
         case "Yes":
         {
              Application.Current.Exit();
              break;
          }
         case "No":
         {
              break;
        }
    }
}

我的问题是在弹出菜单中选择答案之前,我已导航至登录页面.如果我在上面按Yes,则我的应用程序退出,选择No则不执行任何操作.但是我现在在登录页面上. 我尝试使用dlg.Show()而不是dlg.ShowAsync(),但缺少它.因此,我只能使用异步方法,但是在这种情况下,页面导航以在后台登录,因为当我按下后退按钮时,始终会调用NavigationHelper. 有什么问题吗?

My problem is I am navigated to login page before I choose an answer on the popup. If I press Yes on it my app exits, and do nothing when I choose No. But I am on the login page at the moment. I tried to use dlg.Show() instead of dlg.ShowAsync() but it is missing. So I can use only async method, but in this case the page navigate to login in the background, because the NavigationHelper is called always, when I push the back button. What is the problem?

推荐答案

如果这是您真正想做的,请认真考虑.更加用户友好的流程将是保存数据,并让用户退出应用程序并返回.还请考虑如果用户以其他方式(例如,窗口或摄像头按钮)退出,会发生什么情况.

Consider hard if this is what you really want to do. A more user friendly flow would be to save the data and let the user back out of the app and return. Also consider what happens if the user exits in a different way such as the window or camera button.

也就是说,如果这是您唯一的导航处理方法,则列出的代码将起作用(请在BlankPage模板中尝试).我怀疑这不是唯一的,您正在使用诸如BasicPage之类的完整模板中的NavigationHelper之类的东西.在这种情况下,NavigationHelper的GoBackCommand可能会触发并调用Frame.GoBack.

That said, the code you list will work if that's the only navigation handling that you have (try it in the BlankPage template). I suspect that it's not the only and you're using something like the NavigationHelper from the fuller templates such as BasicPage. In that case the NavigationHelper's GoBackCommand is probably triggering and calling Frame.GoBack.

您可以通过在NavigationHelper的GoBackCommand属性上设置自己的RelayCommand来覆盖此行为:

You can override this behaviour by setting your own RelayCommand on the NavigationHelper's GoBackCommand property:

RelayCommand _checkedGoBackCommand;
public BasicPage1()
{
    this.InitializeComponent();

    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
    this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

    _checkedGoBackCommand = new RelayCommand(
                                    () => this.CheckGoBack(),
                                    () => this.CanCheckGoBack()
                                );

    navigationHelper.GoBackCommand = _checkedGoBackCommand;
}

private bool CanCheckGoBack()
{
    return true;
}

private async void CheckGoBack()
{
    Debug.WriteLine("CheckGoBack"); 
    MessageDialog dlg = new MessageDialog("Are you sure you want to quit you will loose all your work ?", "Warning");
    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(CommandHandler1)));
    dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(CommandHandler1)));

    await dlg.ShowAsync();
}

private void CommandHandler1(IUICommand command)
{
    var label = command.Label;
    switch (label)
    {
        case "Yes":
            {
                Application.Current.Exit();
                break;
            }
        case "No":
            {
                break;
            }
    }
}

这篇关于Windows Phone 8.1覆盖特定页面上的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆