Windows Phone 8.1/10/UWP Sidemenu without Splitview

How hard can it be to create a simple sidemenu which is supported on WinRT, Silverlight and UWP apps? Following article is based on a custom self implemented side menu, which is just a prove of concept with lirerly no optimizations and beautiful coding. The following screen recordings access the same code base, just with different XAMLs for the animations.

SideMenuSideMenu2SideMenu3SideMenu4Animation

Basic idea

The main page contains a grid, which split the whole view up into 2 pieces. The side menu and the content view. Based on the interaction the side menu is able to expand itself over the content view. Once interacted with the side menu (click on an icon) only the content view changes. That’s it!

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Kusss"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Core="using:Microsoft.Xaml.Interactions.Core" xmlns:Media="using:Microsoft.Xaml.Interactions.Media" x:Name="page"
    x:Class="Kusss.MasterPage" RequestedTheme="Light" Background="White">
    <Page.Resources>
        <Storyboard x:Name="OpenSideMenu">
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="250">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="SideMenuGrid">
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="250">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="OpenMenuIcon">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CloseMenuIcon">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <PointAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.StartPoint)" Storyboard.TargetName="border">
                <EasingPointKeyFrame KeyTime="0" Value="0,1"/>
                <EasingPointKeyFrame KeyTime="0:0:0.6" Value="0,1">
                    <EasingPointKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingPointKeyFrame.EasingFunction>
                </EasingPointKeyFrame>
            </PointAnimationUsingKeyFrames>
            <PointAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.EndPoint)" Storyboard.TargetName="border">
                <EasingPointKeyFrame KeyTime="0" Value="1,1"/>
                <EasingPointKeyFrame KeyTime="0:0:0.6" Value="1,1">
                    <EasingPointKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingPointKeyFrame.EasingFunction>
                </EasingPointKeyFrame>
            </PointAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                <EasingColorKeyFrame KeyTime="0" Value="#FF919191"/>
                <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FF4D4D4D">
                    <EasingColorKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                <EasingColorKeyFrame KeyTime="0" Value="#FF919191"/>
                <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FFACACAC">
                    <EasingColorKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Offset)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimation Duration="0:0:0.6" To="0" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="MainContentGrid" d:IsOptimized="True">
                <DoubleAnimation.EasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
            <DoubleAnimation Duration="0:0:0.6" To="150" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="MainContentGrid" d:IsOptimized="True">
                <DoubleAnimation.EasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
            <DoubleAnimation Duration="0:0:0.6" To="20" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="MainContentGrid" d:IsOptimized="True"/>
            <DoubleAnimation Duration="0:0:0.6" To="-20" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="SideMenuGrid" d:IsOptimized="True"/>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-20"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-6"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimation Duration="0:0:0.6" To="-6" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="SideMenuGrid" d:IsOptimized="True"/>
        </Storyboard>
        <Storyboard x:Name="CloseSideMenu">
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="250"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="50">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="SideMenuGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="250"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="50">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="OpenMenuIcon">
                <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CloseMenuIcon">
                <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <PointAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.StartPoint)" Storyboard.TargetName="border">
                <EasingPointKeyFrame KeyTime="0" Value="0,1"/>
                <EasingPointKeyFrame KeyTime="0:0:0.6" Value="0,1">
                    <EasingPointKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingPointKeyFrame.EasingFunction>
                </EasingPointKeyFrame>
            </PointAnimationUsingKeyFrames>
            <PointAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.EndPoint)" Storyboard.TargetName="border">
                <EasingPointKeyFrame KeyTime="0" Value="1,1"/>
                <EasingPointKeyFrame KeyTime="0:0:0.6" Value="1,1">
                    <EasingPointKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingPointKeyFrame.EasingFunction>
                </EasingPointKeyFrame>
            </PointAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                <EasingColorKeyFrame KeyTime="0" Value="#FF4D4D4D"/>
                <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FF919191">
                    <EasingColorKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
                <EasingColorKeyFrame KeyTime="0" Value="#FFACACAC"/>
                <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FF919191">
                    <EasingColorKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Offset)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="MainContentGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="150"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="SideMenuGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-20"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="-20"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="MainContentGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="20"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="SideMenuGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="-6"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="border">
                <EasingDoubleKeyFrame KeyTime="0" Value="-6"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Name="UnloadMainContentAnimation">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="MainContentGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="90"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Name="LoadMainContentAnimation">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="MainContentGrid">
                <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Page.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid x:Name="MainContentGrid" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <Grid.RenderTransform>
                <CompositeTransform/>
            </Grid.RenderTransform>
            <Grid.Projection>
                <PlaneProjection/>
            </Grid.Projection>
        </Grid>
        <Border Grid.ColumnSpan="2" x:Name="border" Width="50" HorizontalAlignment="Left" BorderThickness="0,0,3,0" >
            <Border.Projection>
                <PlaneProjection/>
            </Border.Projection>
            <Border.Background>
                <LinearGradientBrush EndPoint="1,1" StartPoint="0,1">
                    <GradientStop Color="#FF919191" Offset="0"/>
                    <GradientStop Color="#FF919191" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
        </Border>
        <Grid Grid.ColumnSpan="2" x:Name="SideMenuGrid" Width="50" HorizontalAlignment="Left">
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.Resources>
                <Style TargetType="StackPanel">
                    <Setter Property="Margin" Value="13, 10, 10, 10" />
                </Style>
                <Style TargetType="Image">
                    <Setter Property="HorizontalAlignment" Value="Left" />
                    <Setter Property="VerticalAlignment" Value="Top" />
                    <Setter Property="Width" Value="40" />
                </Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="FontSize" Value="18" />
                </Style>
            </Grid.Resources>
            <Grid.Projection>
                <PlaneProjection/>
            </Grid.Projection>

            <Image x:Name="OpenMenuIcon" Margin="10" Source="Assets/Hamburger.png" Visibility="Visible">
                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="Tapped">
                        <Media:ControlStoryboardAction Storyboard="{StaticResource OpenSideMenu}"/>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </Image>

            <Image x:Name="CloseMenuIcon" Margin="10" Source="Assets/Hamburger.png" Visibility="Collapsed">
                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="Tapped">
                        <Media:ControlStoryboardAction Storyboard="{StaticResource CloseSideMenu}"/>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </Image>

            <ScrollViewer Margin="0, 15, 0, 0" Grid.Row="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40" />
                    </Grid.RowDefinitions>

                    <StackPanel Grid.Row="0" Orientation="Horizontal" Tapped="ChangeContent">
                        <Image Source="Assets/WeekTimeTableSideMenu.png" />
                        <TextBlock Text="Week view" />
                    </StackPanel>
                </Grid>
            </ScrollViewer>
        </Grid>
    </Grid>
</Page>

 

Navigation

Because there is no page navigation, the main page has to handle each hardware input by its own. A very simple navigation stack gets that work done properly.

        public class NavigationHelper
        {
            private Stack<UIElement> navigationStack = new Stack<UIElement>(5);
            private UIElement nextNavigationElement = null;

            public NavigationHelper(Storyboard unloadAnimation, Storyboard loadAnimation, Grid mainContentGrid)
            {
                UnloadAnimation = unloadAnimation;
                LoadAnimation = loadAnimation;
                MainContentGrid = mainContentGrid;

                UnloadAnimation.Completed += (a, b) => StartLoadAnimation();
            }

            public Storyboard UnloadAnimation { get; private set; }

            public Storyboard LoadAnimation { get; private set; }

            public Grid MainContentGrid { get; private set; }

            public string LastFocusedPage { get; private set; }

            public void NagivateToPage<T>(string key) where T : UIElement
            {
                NavigateToPage<T>(key, null);
            }

            public void NavigateToPage<T>(string key, object parameter) where T : UIElement
            {
                if (nextNavigationElement != null)
                    navigationStack.Push(nextNavigationElement);

                //If the user clicked on the same element, we dont care.
                if (key == LastFocusedPage)
                    return;

                LastFocusedPage = key;
                nextNavigationElement = Activator.CreateInstance<T>();
                if (parameter != null) ((IAcceptParameter)nextNavigationElement).Accept(parameter);
                UnloadAnimation.Begin();
            }

            private void StartLoadAnimation()
            {
                if (MainContentGrid.Children.Count > 0)
                    MainContentGrid.Children.RemoveAt(0);
                MainContentGrid.Children.Add(nextNavigationElement);
                LoadAnimation.Begin();
            }

            public void NavigateBack()
            {
                if (navigationStack.Count <= 0)
                {
                    //App.Exit does not close the application immediately?!
                    Application.Current.Exit();
                    return;
                }

                nextNavigationElement = navigationStack.Pop();
                UnloadAnimation.Begin();
            }

            /// <summary>
            /// It does what it says.
            /// </summary>
            public void RemoveLastElementFromNavigationStack()
            {
                if (navigationStack.Count >= 1)
                    navigationStack.Pop();
            }
        }

Animations

For the animations I used Blend vor VS 2015. The XAML code for the main page consists of 90% animations, 10% actual code.

Disadvantages

  • No actual page navigation. Content view is just hot swapped
  • Because of the missing page navigation, most methods won’t be called on startup. (OnNavigatedTo…)

Advantages

  • Every UIElement is content view compatible
  • 100% control over the navigation stack, without relying on pre implementations
  • Very easy to customize and change the UI layout.
0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments