Using Panorama Control In Message Box

This article explains how to show a Panorama Control in a message box.

Introduction

In this article I will show you how to place a Panorama Control in a message box. To create this you have two choices, one is to create the Panorama using C# and the other one is to create it using XAML. The latter one looks more promising and user-friendly. I'll also use the Custom Message Box control of the Windows Phone toolkit. In our app we can use the default message box to show alerts or messages to the user but it is very limited. Like we won't be able to show a scrollable message, hyperlinks and other controls . The Windows Phone 8 toolkit removes the limitations of the default message box by providing a Custom Message Box control. So let's get started.

Custom Message Box

Custom Message Box is a control provided in the Windows Phone 8 toolkit for customizing the interface and content of the message box. It enables you to add various UI elements in the message box that is not possible in the default message box. In this demo , we will create our UI from XAML and use the C# to position it in a message box.

In the following demo I have used the following properties of the Custom Message Box:

  • Content
    It takes an object as a value. It is displayed in the message box as we see text in our default message box. String is also an object and can be used for adding text to a message box. 

  • Height 
    To set the height of the message box. It can be set or left to auto-fit to the content size. 

  • Opacity
    To set the opacity of the message box. Value ranging from 0 to 1.  

  • Left Button Content
    It also takes an object as a value. It is the left message button of the message box.  

  • Right Button Content
    It also takes an object as a value. It is the right message button of the message box. 

Apart from the properties, the following method is also used:

  • Show
    This method is used to show the message box. 

  • SetIsTiltEnabled
    To enable a tilt effect on the control.

To handle the user response to the dialog we are handling the click event of dialog buttons. For this I'm handling a dialog dismissing event. In the handler I'm comparing the dialog result to do the various actions on user response.

Before you can run the following demo you need to install a Windows Phone 8 toolkit in your project. It's very easy to do this.

Installing Windows Phone 8 Toolkit

Use the following procedure to install the Windows Phone 8 toolkit using Nugget:

  1. Open your app project (either an existing one or a new one). 

  2. From the toolbar select "Tools" -> "NuGet Package Manager" -> "Package Manager Console". 

  3. Now in the Package Manager Console type the following:

    Install-Package WPtoolkit

    After typing this you will probably see the log similar to this:

    install-package wptoolkit
    'WPtoolkit 4.2013.08.16' already installed.
    Adding 'WPtoolkit 4.2013.08.16' to Demo.
    Successfully added 'WPtoolkit 4.2013.08.16' to Demo. 

  4. The next step is to add its reference to the XAML page. To do this just add this line:

    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

Demo

The following code places a Panorama Control inside the message box control. To go step-by-step, first create a panorama using XAML code. Name that panorama to pan. Once you complete the design of the panorama, set its visibility to collapsed. In the code behind, we are doing the following:

  • Capture the instance of the Panorama in main page constructor. 

  • Remove the panorama from page. 

  • Add the Panorama in the message box and set its visibility to visible. 

Let's see how to code it.

XAML

<phone:PhoneApplicationPage

    x:Class="Demo.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

 

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock  Text="Demo" Margin="9,-7,0,0" FontSize="40" />

        </StackPanel>

 

        <!--ContentPanel - place additional content here-->

        <StackPanel Name="ctr" Orientation="Vertical" Margin="0,117,0,28">

        <Button Content="Open Panorma" Click="openMsgBox" Height="253" VerticalAlignment="Bottom"/>

        <phone:Panorama  Name="pan" Height="382" Margin="0,0,10,0">

            <phone:PanoramaItem Header="page1">

                <TextBlock FontSize="75" TextWrapping="Wrap" TextAlignment="Center" Foreground="Red" Text="This is first page"></TextBlock>

            </phone:PanoramaItem>

            <phone:PanoramaItem Header="page2">

                <TextBlock FontSize="75" TextWrapping="Wrap" TextAlignment="Center" Foreground="Red" Text="This is second page"></TextBlock>

            </phone:PanoramaItem>

            <phone:PanoramaItem  Header="page3">

                <TextBlock FontSize="75" TextWrapping="Wrap" TextAlignment="Center" Foreground="Red" Text="This is third page"></TextBlock>

            </phone:PanoramaItem>

        </phone:Panorama>

        </StackPanel>

    </Grid>

 

</phone:PhoneApplicationPage>

C# Code Behind

using Microsoft.Phone.Controls;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Diagnostics;

using Microsoft.Phone.Tasks;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.IO;

 

namespace Demo

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

 

            //Capture panorama instance

            p = pan;

 

            //Remove from page

            ctr.Children.Remove(pan);

        }

 

        Panorama p = new Panorama();

        CustomMessageBox cmb;

        Grid g;

 

        private void openMsgBox(object sender, RoutedEventArgs e)

        {

            //Create new grid.

            g = new Grid();

 

            //add panorama in grid

            g.Children.Add(p);

       

            // Create new custom message box instance

            cmb = new CustomMessageBox()

            {

                // Set its content to our grid

                Content = g,

 

                Opacity = 0.98,

 

                Height = 500,

 

                // Left button of message box Button

                LeftButtonContent = "Got it.",

 

                //Right button of message Box

                RightButtonContent = "Next",

 

            };

 

            //enable tilt effect

            TiltEffect.SetIsTiltEnabled(p, true);

 

            //Handle msg box closing

            cmb.Dismissing += cmb_Dismissing;

 

 

            //Show the message box...

            cmb.Show();

        }

 

        void cmb_Dismissing(object sender, DismissingEventArgs e)

        {

            if (e.Result == CustomMessageBoxResult.LeftButton)

            {

                //Handle left click

            }

            if (e.Result == CustomMessageBoxResult.RightButton)

            {

                //Handle right-click

 

            }

 

            //remove the panorama from the visual tree to avoid duplicates.

            g.Children.Remove(p);

        }

    }

}

 

The Show method is used to launch the task.

 

Output