CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Animate Page Load, Page Unload for Windows Phone

1079 Views   
public ItemViewer() { InitializeComponent(); Loaded += ItemViewer_Loaded; } void ItemViewer_Loaded(object sender, RoutedEventArgs e) { if (LayoutRoot.Visibility != Visibility.Collapsed) throw new Exception("item.Visibility != Collapsed"); var _TopFrom = this.RenderSize.Height; var _TopTo = 0; var _OpacityFrom = 0; var _OpacityTo = 1; ShowHide(LayoutRoot, null, _TopFrom, _TopTo, _OpacityFrom, _OpacityTo); } protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { var _TopFrom = 0; var _TopTo = this.RenderSize.Height; var _OpacityFrom = 1; var _OpacityTo = 0; e.Cancel = true; base.OnBackKeyPress(e); ShowHide(LayoutRoot, () => { NavigationService.GoBack(); }, _TopFrom, _TopTo, _OpacityFrom, _OpacityTo); } private static void ShowHide(UIElement item, Action callback, double topFrom, double topTo, double opacityFrom, double opacityTo) { // setup var _Translate = new TranslateTransform { X = 0, Y = topFrom }; item.RenderTransform = _Translate; item.Visibility = Visibility.Visible; item.Opacity = opacityFrom; // animate var _Storyboard = new Storyboard(); var _Duration = TimeSpan.FromSeconds(.5); // opacity var _OpacityAnimate = new DoubleAnimation { To = opacityTo, Duration = _Duration, }; _Storyboard.Children.Add(_OpacityAnimate); Storyboard.SetTarget(_OpacityAnimate, item); Storyboard.SetTargetProperty(_OpacityAnimate, new PropertyPath(UIElement.OpacityProperty)); // translate (location) var _TranslateAnimate = new DoubleAnimation { To = topTo, Duration = _Duration, EasingFunction = new SineEase { EasingMode = EasingMode.EaseOut } }; _Storyboard.Children.Add(_TranslateAnimate); Storyboard.SetTarget(_TranslateAnimate, _Translate); Storyboard.SetTargetProperty(_TranslateAnimate, new PropertyPath(TranslateTransform.YProperty)); // finalize _TranslateAnimate.Completed += (s, arg) => { if (callback != null) callback(); }; _Storyboard.Begin(); }
by Jerry Nixon
  January 03, 2012 @ 5:06pm
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies