How to handle the back button on Windows Phone 7

后端 未结 3 503
时光取名叫无心
时光取名叫无心 2020-12-01 01:04

On the windows phone 7 emulator, when the hardware back button is pressed, the default behaviour is for it to close your current application. I want to override this defaul

3条回答
  •  一整个雨季
    2020-12-01 02:08

    I was able to use this technique to do what I wanted, which is to prevent back navigation while hiding a control that slides in and out of the window. By default, the control's visibility is collapsed. Storyboards are used to control when it becomes visible or collapsed. In XAML, inside the Storyboard:

    
    
        
            
                Visible
            
        
    
    

    Then in the page's 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 (which is a grid), the KeyTime is set to "00:00:01" because I want it to remain visible while it is sliding (and fading) out of view.

    Note 2: The reason StoryboardHideControlSlider.GetCurrentState() != ClockState.Active is included in the if statement is because if the user hits the back button twice and the Storyboard hasn't completed it will run again. This prevents the backbutton cancelling navigation back to the previous page. So in other words, if the Storyboard is active, the code "knows" that the user has already initiated hiding it and intends to navigate back to the previous page. (Well, at least that's behavior they're going to get...and they won't see the animation twice)!

提交回复
热议问题