0

In my application there is a page with one check box.

What I want is that whenever user press back button button.

If check box is checked then un check it

If check box is not checked then perform its functionality, like going back.

Updated:

This line is in page constructor.

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

This is event.

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;
    }
}

this code is running on bac press, but after this code executes the application page go back too.

I think its worth telling you that I am using basic page, which has these lines of code by default.

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;
    }

I think this variable is handling navigation some where but un able to identify.

Yawar
  • 1,924
  • 3
  • 29
  • 39

2 Answers2

1

You can use Windows.Phone.UI.Input.HardwareButtons.BackPressed event as suggested here.

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

Question seems to be a duplicate though.

Update:

Since you are using the NavigationHelper class, this class handles the BackPressed event and performs navigation by it's own:

 /// <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);
    }
 }

So setting e.Handled to true has no effect in this case. In order to have control over navigation, you may edit the NavigationHelper class (it exists in "Common" directory of the project).

First, replace that part of the class with this:

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);
      }
   }
}

Then use the new defined BackPressed event of the NavigationHelper class in your page:

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;
    }
}
Community
  • 1
  • 1
Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
0

At last found this post to control the back button navigation and its working perfectly, Mehrzad Chehraz answer was similar to this but not completed.

Yawar
  • 1,924
  • 3
  • 29
  • 39