1. 程式人生 > >WUP 頁面跳轉過度動畫

WUP 頁面跳轉過度動畫

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動畫,會導致在彈出頁面中再次進行的導航後後退沒有動畫(按物理導航按鈕時)