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)!
Stonetip Mar 05 '11 at 16:33 2011-03-05 16:33
source share