How to handle the back button on Windows Phone 7

On Windows Phone 7 emulator, when the back button is pressed, it is used by default to close your current application. I want to override this default behavior so that it jumps to the previous page in my application.

After some research, it seems that this can be done by overriding the OnBackKeyPress method, for example:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { // do some stuff ... // cancel the navigation e.Cancel = true; } 

However, clicking the back button still closes my application. Setting a breakpoint on the method described above shows that it is never called. I have another breakpoint on my application exit code, and that breakpoint hits.

Is there anything else I need to do to intercept the back button?

+43
windows-phone-7
May 20 '10 at 11:07
source share
3 answers

It seems that it is not possible to override the OnBackKeyPress method to intercept the return key unless you use the Navigate method to move between pages in your application.

My previous navigation method was to change the root visualization, for example:

 App.Current.RootVisual = new MyPage(); 

This meant that I could store all my pages in memory, so I did not need to cache the data stored on them (some data is collected over the network).

Now it seems to me that I need to use the Navigate method on the frame of the page, which creates a new instance of the page I'm moving to.

 (App.Current.RootVisual as PhoneApplicationFrame).Navigate( new Uri("/MyPage.xaml", UriKind.Relative)); 

Once I started navigating using this method, I could then override the reverse control button in the manner described in my question ...

+29
May 20, '10 at 15:50
source share

If you do not want the default behavior, set Cancel = true in the CancelEventArgs parameter in OnBackKeyPress. On my page, I redefined the back button to close the web browser control, and not go back.

  protected override void OnBackKeyPress(CancelEventArgs e) { if (Browser.Visibility == Visibility.Visible) { Browser.Visibility = Visibility.Collapsed; e.Cancel = true; } } 
+23
May 28 '11 at
source share

I was able to use this technique to do what I wanted to prevent reverse navigation by hiding a control that slides in and out of the window. By default, control visibility is minimized. Storyboards are used to control when they become visible or crumble. In XAML inside the Storyboard:

 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ControlScroller" Storyboard.TargetProperty="(UIElement.Visibility)"> <ObjectAnimationUsingKeyFrames.KeyFrames> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames.KeyFrames> 

Then in the page code:

 protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { if(ControlScroller.Visibility == Visibility.Visible && StoryboardHideControlSlider.GetCurrentState() != ClockState.Active) { StoryboardHideControlSlider.Begin(); ContentGrid.IsHitTestVisible = true; e.Cancel = true; } } 

Note. In the storyboard that hides the ContentScroller (it's a grid), KeyTime is set to โ€œ00:00:01โ€ because I want it to remain visible when it slides (and disappears) out of sight.

Note 2: The reason StoryboardHideControlSlider.GetCurrentState() != ClockState.Active included in the if statement, because if the user presses the back button twice and the storyboard is not completed, it will start again. This prevents canceling navigation back to the previous page. In other words, if the Storyboard is active, the code โ€œknowsโ€ that the user has already begun to hide it and intends to return to the previous page. (Well, at least theyโ€™re going to get this behavior ... and they wonโ€™t see the animation twice)!

+3
Mar 05 '11 at 16:33
source share



All Articles