UWP 頁面跳轉過度動畫

0.兩個動畫的定義
TIP:說明下這裏的childrenFrameCompositeTransform這是給Frame的CompositeTransform的名字
以前從來不定義名字,應爲有取名綜合徵。最近我又嫌棄這搞法太長,經常打字缺斤少兩的。

Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)"

現在短了多清爽

<!--#region 子框架進入動畫-->
        <Storyboard x:Name="childrenInStoryboard"
                    x:FieldModifier="public"
                    Completed="childrenInStoryboard_Completed">
            <DoubleAnimation x:Name="childrenInDA"
                             Storyboard.TargetName="childrenFrameCompositeTransform"          
                             Storyboard.TargetProperty="TranslateX"
                             To="0"
                             Duration="0:0:0.5">
                <DoubleAnimation.EasingFunction>
                    <QuarticEase  EasingMode="EaseIn"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
        <!--#endregion-->
        <!--#region 子框架退出動畫-->
        <Storyboard x:Name="childrenOutStoryboard"
                    x:FieldModifier="public"
                    Completed="childrenOutStoryboard_Completed">
            <DoubleAnimation x:Name="childrenOutDA"
                             Storyboard.TargetName="childrenFrameCompositeTransform"
                             Storyboard.TargetProperty="TranslateX"
                             From="0"
                             Duration="0:0:0.5">
                <DoubleAnimation.EasingFunction>
                    <QuarticEase  EasingMode="EaseInOut"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
        <!--#endregion-->

1.使用Frame本身就有的Navigating和navigated事件來執行開閉動畫。

        /// <summary>
        /// childrenFrame導航開始前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void childrenFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            childrenFrameCompositeTransform.TranslateX = 0;
            //1.設置childrenFrame導航進入動畫
            EdgeUIThemeTransition inStoryoard = new EdgeUIThemeTransition();
            //2.只有在導航是新實例才執行動畫
            if (e.NavigationMode == NavigationMode.New)
            {
                #region 系統定義的邊緣UI
                //inStoryoard.Edge = EdgeTransitionLocation.Right;
                //TransitionCollection tc = new TransitionCollection();
                //tc.Add(inStoryoard);
                //childrenFrame.ContentTransitions = tc;
                #endregion
                #region 自定義動畫
                childrenInDA.From = this.ActualWidth;
                childrenInStoryboard.Begin();
                #endregion
            }
        }
        /// <summary>
        /// 導航結束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void childrenFrame_Navigated(object sender, NavigationEventArgs e)
        {
            var tempPageE = e.Content as Page;
            switch (tempPageE.Tag.ToString())
            {
                case "安全頁面":
                    (childrenFrame.Content as SafePage).BackEvent += MainPage_BackEvent;
                    break;
                default:
                    break;
            }
        }

2.在所有需要的退出的動畫的頁面實現BackEvert委託
例如:

public sealed partial class SafePage : Page
    {
        internal delegate void GoBackHandler();
        internal event GoBackHandler BackEvent;

        public SafePage()
        {
            this.InitializeComponent();
        }
    }

3.在MainPage.cs中實現MainPage_BackEvent

        /// <summary>
        /// 實現各個頁面定義的委託
        /// </summary>
        private void MainPage_BackEvent()
        {
            //1.設置退出動畫的TO屬性
            childrenOutDA.To = this.ActualWidth;
            //2.動畫開始
            childrenOutStoryboard.Begin();

        }

3.還有一點要在退出動畫結束的時候重置一些數據

 /// <summary>
        /// childrenFrame退出動畫結束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void childrenOutStoryboard_Completed(object sender, object e)
        {
            childrenFrame.ContentTransitions = null;
            childrenFrame.Content = null;
            childrenFrame.SetNavigationState(navInfo);
            childrenFrameCompositeTransform.TranslateX = 0;
        }

順手再來個雙擊退出

在Mainpage中註冊後退管理事件

/// <summary>
        /// MainPage載入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //1.註冊後退管理
            SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
        }

TIP:雖然SystemNavigationManager.GetForCurrentView().BackRequested是系統級別的事件最好是+=了就-=我這裏沒這麼做。因爲我這個是當全局來用的。APP退出了也就沒那必要了不是。

bool IsExit = false;
        /// <summary>
        /// 響應後退按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MainPage_BackRequested(object sender, BackRequestedEventArgs e)
        {
            if (childrenFrame.Content != null)
            {
                e.Handled = true;
                if (childrenFrame.CanGoBack)
                {
                    childrenFrame.GoBack();
                }
                else
                {
                    childrenOutDA.To = this.ActualWidth;
                    childrenOutStoryboard.Begin();
                }
            }
            else
            {
                if (e.Handled == false)
                {
                    if (IsExit)
                    {
                        //1.退出APP
                        Application.Current.Exit();
                    }
                    else
                    {
                        IsExit = true;
                        e.Handled = true;
                        ExitTipTextBlock.Text = "再按一次(,,•∀•)ノ゛ByeBye";
                        ExitTipBorder.Visibility = Visibility.Visible;
                        await Task.Delay(1500);
                        IsExit = false;
                        ExitTipBorder.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {

                }
            }
        }

歡迎大家指點更好的方法。(o'ー'o)
這個辦法在可以導航的時候時 執行的是childrenOutStoryboard動畫,會導致在彈出頁面中再次進行的導航後後退沒有動畫(按物理導航按鈕時)

http://www.cnblogs.com/Enious/p/5858333.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章