切换动画,相关的过渡动画,是淡入淡出的效果
private void Frame_Navigating(object sender, NavigatingCancelEventArgs e) { if (Content != null && !_allowDirectNavigation) { e.Cancel = true; _navArgs = e; this.IsHitTestVisible = false; DoubleAnimation da = new DoubleAnimation(0.3d, new Duration(TimeSpan.FromMilliseconds(100))); da.Completed += FadeOutCompleted; this.BeginAnimation(OpacityProperty, da); } _allowDirectNavigation = false; } private void FadeOutCompleted(object sender, EventArgs e) { (sender as AnimationClock).Completed -= FadeOutCompleted; this.IsHitTestVisible = true; _allowDirectNavigation = true; switch (_navArgs.NavigationMode) { case NavigationMode.New: if (_navArgs.Uri == null) { this.myFrame.NavigationService.Navigate(_navArgs.Content); } else { this.myFrame.NavigationService.Navigate(_navArgs.Uri); } break; case NavigationMode.Back: this.myFrame.NavigationService.GoBack(); break; case NavigationMode.Forward: this.myFrame.NavigationService.GoForward(); break; case NavigationMode.Refresh: this.myFrame.NavigationService.Refresh(); break; } Dispatcher.BeginInvoke(DispatcherPriority.Loaded, (ThreadStart)delegate() { DoubleAnimation da = new DoubleAnimation(1.0d, new Duration(TimeSpan.FromMilliseconds(200))); this.BeginAnimation(OpacityProperty, da); }); } private bool _allowDirectNavigation = false; private NavigatingCancelEventArgs _navArgs = null;
场景:某个导航页面中的按钮点击完成之后使Frame的Visibility为Collapsed
最初始的想法是从Page页面中获取宿主控件然后对宿主控件即Frame直接进行操作
查阅MSDN之后没有找到相对应的方法或者属性,只能修改宿主的长宽及ICON
所以变通的想到使用导航结束的事件来传递消息,来使Frame能够获取想要传达的信息,需要按照特定约定
约定:当导航结束后,myFrame_Navigated事件中查看Frame的Content如果是Page且Visibility为Collapsed则改变Frame的Visibility
private void myFrame_Navigated(object sender, NavigationEventArgs e) { Page page = this.myFrame.Content as Page; if (page == null) return; if (page.Visibility == Visibility.Collapsed) { this.Visibility = Visibility.Collapsed; this.myFrame.NavigationService.GoBack(); } }