Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A ViewStack Component for Silverlight 2 with Transitions and Cache management

0.00/5 (No votes)
20 Oct 2008 2  
An enhanced ViewStack component for Silverlight 2 with transitions and cache management.

Introduction

This is a continuation to my previous article, ViewStack component for Silverlight 2 – An inevitable control in RIA space. I have added Transitions and Cache management feature to our ViewStack control. Please see DShop. Hopefully, Microsoft will add more navigator controls like this in the next release. I hope this control will be useful for the Silverlight community. Please don't hesitate to provide your valuable comments.

Transitions

We can now apply the following transitions on pages placed in our ViewStack, I will be adding more effects soon.

  • FadeTransition
  • WipeTransition

I have followed the ideas demonstrated at flawlesscode.com in their article titled Silverlight 2 Navigating Between XAML Pages, for implementing transitions in our ViewStack. I would like to express my sincere thanks for the post.

Cache Management

Pages inside the ViewStack can be configured to be cached or not by setting the Cache property on the ViewInfo object. When the caching feature is turned on, a page once activated will be kept in the cache, and the visibility of the page will be just toggled by setting the Canvas.SetZIndex property during subsequent activations. A fresh instance of the page/usercontrol will be created and displayed if the caching feature is not enabled. The default behaviour is true (i.e., cache).

Using the Code

Page.xaml

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SilverlightApplication1.Control" 
    xmlns:Transitions="clr-namespace:SilverlightApplication1.Utils.Transitions">
    <Grid x:Name="LayoutRoot" Background="Wheat"  >
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="50" />
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
         </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="Home" Content="Home" 
           Grid.Row="1" Grid.Column="1" Click="Home_Click"/>
        <Button x:Name="AboutUS" Content="AboutUS" 
           Grid.Row="1" Grid.Column="3" Click="AboutUS_Click"/>
         <Grid Grid.Row="3" Grid.ColumnSpan="4" Grid.Column="1">
                <local:ViewStack x:Name="myViewStack" 
                   SelectedViewName="{Binding Path=SelectedViewName,Mode=TwoWay}"  >
                 <local:ViewStack.Views>
                    <local:ViewInfo ViewName="Home" 
                          ViewTypeName="SilverlightApplication1.HomeView" 
                          Cache="False" >
                        <local:ViewInfo.Transition>
                            <Transitions:WipeTransition 
                               Direction="TopToBottom" Duration="1"/>
                        </local:ViewInfo.Transition>
                    </local:ViewInfo>
                    <local:ViewInfo ViewName="AboutUS" 
                          ViewTypeName="SilverlightApplication1.AboutUSView" 
                          Cache="False">
                        <!--<local:ViewInfo.Transition>
                            <Transitions:FadeTransition 
                                Duration="1"></Transitions:FadeTransition>
                        </local:ViewInfo.Transition>-->
                        <local:ViewInfo.Transition>
                            <Transitions:WipeTransition 
                               Direction="LeftToRight" Duration="1"/>
                        </local:ViewInfo.Transition>
                    </local:ViewInfo>
                </local:ViewStack.Views>
                </local:ViewStack>
         </Grid>
    </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication1.Control;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = ModelLocator.Instance;
            ModelLocator.Instance.SelectedViewName = "Home";
            
        }


        private void Home_Click(object sender, RoutedEventArgs e)
        {
            ModelLocator.Instance.SelectedViewName = "Home";
        }

        private void AboutUS_Click(object sender, RoutedEventArgs e)
        {
            ModelLocator.Instance.SelectedViewName = "AboutUS";
        }
    }
}

ViewStack.JPG

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here