在winrt windows phone应用程序中超越后退按钮功能

时间:2015-04-22 08:56:58

标签: windows-runtime windows-phone-8.1 winrt-xaml

在我的应用程序中,有一个带有一个复选框的页面。

我想要的是每当用户按下后退按钮按钮时。

如果选中复选框,则取消选中

如果未选中复选框,则执行其功能,如返回。

更新

这一行在页面构造函数中。

Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

这是事件。

void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (RechargeAccountPivot.SelectedIndex == 2 && ePayBorder.Visibility == Windows.UI.Xaml.Visibility.Visible)
    {
        ePayBorder.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        e.Handled = true;
    }
}

此代码在bac上运行,但在此代码执行后,应用程序页面也会返回。

我认为值得告诉你我正在使用基本页面,默认情况下会有这些代码行。

private NavigationHelper navigationHelper;
public RechargeAccount()
    {
        this.InitializeComponent();

        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }

我认为这个变量正在处理导航,但无法识别。

2 个答案:

答案 0 :(得分:1)

您可以按建议使用Windows.Phone.UI.Input.HardwareButtons.BackPressed事件here

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) {
   if (checkbox.IsChecked) {
       checkbox.IsChecked = false;
       e.Handled = true;
   }
}

问题似乎是重复的。

<强>更新

由于您使用的是NavigationHelper类,因此该类处理BackPressed事件并通过它自己执行导航:

 /// <summary>
 /// Invoked when the hardware back button is pressed. For Windows Phone only.
 /// </summary>
 /// <param name="sender">Instance that triggered the event.</param>
 /// <param name="e">Event data describing the conditions that led to the event.</param>
 private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) {
    if (this.GoBackCommand.CanExecute(null)) {
       e.Handled = true;
       this.GoBackCommand.Execute(null);
    }
 }

因此将e.Handled设置为true在这种情况下无效。为了控制导航,您可以编辑NavigationHelper类(它存在于项目的“Common”目录中)。

首先,用这个替换该类的那部分:

public event EventHandler<Windows.Phone.UI.Input.BackPressedEventArgs> BackPressed;
private void OnBackPressed(Windows.Phone.UI.Input.BackPressedEventArgs e) {
   if (this.BackPressed != null) {
      this.BackPressed(this, e);
   }
}
/// <summary>
/// Invoked when the hardware back button is pressed. For Windows Phone only.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) {
   this.OnBackPressed(e);
   if (!e.Handled) {
      if (this.GoBackCommand.CanExecute(null)) {
         e.Handled = true;
         this.GoBackCommand.Execute(null);
      }
   }
}

然后在页面中使用NavigationHelper类的新定义的BackPressed事件:

private NavigationHelper navigationHelper;
public RechargeAccount()
{
    this.InitializeComponent();

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

void NavigationHelper_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (RechargeAccountPivot.SelectedIndex == 2 && ePayBorder.Visibility == Windows.UI.Xaml.Visibility.Visible)
    {
        ePayBorder.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        e.Handled = true;
    }
}

答案 1 :(得分:0)

最后发现this帖子来控制后退按钮导航并且它的工作完美,Mehrzad Chehraz的答案与此类似但未完成。