stop wp7 out transitions while canceling navigatio

2019-08-23 01:52发布

问题:

In some case in my application, I have to cancel back navigation. I'm doing it with code:

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Back)
        {
            if (WaitPanelGrid.Visibility == System.Windows.Visibility.Visible)
            {
                e.Cancel = true;
            }
        }
        base.OnNavigatingFrom(e);
    }

And everything is OK, except one thing: In this page i have transitions:

<toolkit:TransitionService.NavigationInTransition>
    <toolkit:NavigationInTransition>
        <toolkit:NavigationInTransition.Backward>
            <toolkit:TurnstileTransition Mode="BackwardIn" />
        </toolkit:NavigationInTransition.Backward>
        <toolkit:NavigationInTransition.Forward>
            <toolkit:TurnstileTransition Mode="ForwardIn" />
        </toolkit:NavigationInTransition.Forward>
    </toolkit:NavigationInTransition>
</toolkit:TransitionService.NavigationInTransition>
<toolkit:TransitionService.NavigationOutTransition>
    <toolkit:NavigationOutTransition>
        <toolkit:NavigationOutTransition.Backward>
            <toolkit:TurnstileTransition Mode="BackwardOut" />
        </toolkit:NavigationOutTransition.Backward>
        <toolkit:NavigationOutTransition.Forward>
            <toolkit:TurnstileTransition Mode="ForwardOut" />
        </toolkit:NavigationOutTransition.Forward>
    </toolkit:NavigationOutTransition>
</toolkit:TransitionService.NavigationOutTransition>

And calling Cancel in OnNavigatingFrom stop application from navigate back, but transition is still working, so in the end i have just black screen, because page was not changed, but it was animated and moved somewhere behind screen.

My question is: How I can cancel navigating AND transition?

回答1:

Excerpt from this article

Disabling navigation transitions

If you want to disable navigation transitions for a Page, set TransitionService.NavigationInTransition and TransitionService.NavigationOutTransition to null on the Page. If you want to restore those values, just store them beforehand and then set them again.

PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;

PhoneApplicationPage page = (PhoneApplicationPage)frame.Content;

// Save the transitions

NavigationInTransition oldIn = TransitionService.GetNavigationInTransition(page);

NavigationOutTransition oldOut = TransitionService.GetNavigationOutTransition(page);

// Clear the transitions

TransitionService.SetNavigationInTransition(page, null);

TransitionService.SetNavigationOutTransition(page, null);

// Restore the transitions

TransitionService.SetNavigationInTransition(page, oldIn);

TransitionService.SetNavigationOutTransition(page, oldOut);