Click here to Skip to main content
16,004,458 members
Articles / Desktop Programming / XAML

A ViewStack Component for Silverlight 2 with Transitions and Cache management

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
20 Oct 2008CPOL1 min read 23.8K   6  
An enhanced ViewStack component for Silverlight 2 with transitions and cache management.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication1.Utils.Transitions
{
    public class WipeTransition : TransitionBase
    {


        private TimeSpan time;
        private UserControl newPage;
        private UserControl oldPage;
   
        private Brush oldOpacityMaskBrush;
        public WipeTransition()
            :this(WipeDirection.LeftToRight)
        {
        }

        public WipeTransition(WipeDirection direction, TimeSpan duration)
        {
            time = duration;
            this.direction = direction;
        }
        public WipeTransition(WipeDirection direction)
            : this(direction, TimeSpan.FromSeconds(.5))
        { }


        private WipeDirection direction;
        public WipeDirection Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        private double timeAsDouble;
        public Double Duration
        {
            get { return timeAsDouble; }
            set {
                timeAsDouble = value;
                time = TimeSpan.FromSeconds(timeAsDouble);
                }
        }


        public override void PerformTranstition(UserControl newPage, UserControl oldPage)
        {
           
            this.newPage = newPage;
            this.oldPage = oldPage;
            if (newPage == null) return;

            
            Storyboard sb;
             
            GradientStopCollection gradientStopCollection = new GradientStopCollection();
            GradientStop gradientStop1 = new GradientStop();
            gradientStop1.Color = Colors.Black;
            gradientStop1.Offset = 0;

            GradientStop gradientStop2 = new GradientStop();
            gradientStop2.Color = Colors.Transparent;
            gradientStop2.Offset = 0;

            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(gradientStopCollection, 0);
          

            switch (direction)
            {
                case WipeDirection.LeftToRight:
                    linearGradientBrush.StartPoint = new Point(0, 0);
                    linearGradientBrush.EndPoint = new Point(1, 0);
                    break;
                case WipeDirection.RightToLeft:
                    linearGradientBrush.StartPoint = new Point(1, 0);
                    linearGradientBrush.EndPoint = new Point(0, 0);
                    break;
                case WipeDirection.TopToBottom:
                    linearGradientBrush.StartPoint = new Point(0, 0);
                    linearGradientBrush.EndPoint = new Point(0, 1);
                    break;
                case WipeDirection.BottomToTop:
                    linearGradientBrush.StartPoint = new Point(0, 1);
                    linearGradientBrush.EndPoint = new Point(0, 0);
                    break;
            }



            gradientStopCollection.Add(gradientStop1);
            gradientStopCollection.Add(gradientStop2);

            oldOpacityMaskBrush = newPage.OpacityMask;
            newPage.OpacityMask = linearGradientBrush;
            
            Duration duration = new Duration(time);

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 0;
            animation.To =1.1;
            animation.By = 1;
            animation.Duration = duration;


            DoubleAnimation animation1 = new DoubleAnimation();
            animation1.From = 0;
            animation1.To = 1.1;
            animation1.By = 1;
            animation1.Duration = duration;


            sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(animation);
            sb.Children.Add(animation1);
            sb.Completed += sb_Completed;

            Storyboard.SetTarget(animation, gradientStop1);
            Storyboard.SetTargetProperty(animation, new PropertyPath("OffSet"));

            Storyboard.SetTarget(animation1, gradientStop2);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("OffSet"));
            
            sb.Begin();

        }

        void sb_Completed(object sender, EventArgs e)
        {
            newPage.OpacityMask = oldOpacityMaskBrush;
            OnTransitionCompleted(newPage, oldPage);
        }

        public enum WipeDirection
        {
            LeftToRight,
            RightToLeft,
            TopToBottom,
            BottomToTop
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Digital Mesh Softech India (P) Ltd.
India India
Started my career in software development in the year 1996 and still trying to learn and implement new technologies. Currently pursuing my career as Technical Architect @ Digital Mesh Softech India (P) Ltd (www.digitalmesh.co.in)

Comments and Discussions