Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 08:43, 9 June 2014 by croozeus (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to create customized Popup in WP8

From Wiki
Jump to: navigation, search
Featured Article
08 Jun
2014

This article explains how to create a customised Popup dialog in Windows Phone.

Article Metadata
Tested with
SDK: Windows Phone 8.0 SDK
Article
Created: munjalrohit (07 May 2014)
Last edited: croozeus (09 Jun 2014)

Contents

Introduction

Although Popup control are available in WP8 development but sometimes we need to create brand specific popup dialogs. This article shows how to create a popup user control, which displays a message, shows OK and Cancel buttons and allows to handle these events. Also, included in the user control is the "Imagebuttoncontrol" which gives image button with mouse over animation look, but the main target is for popup.

The component is reusable and can be used in any Windows Phone 8 projects.

Screenshots

Popup1.png Popup2.png

Explanation

This code contain user control "UCPopup" where i have designed for popup screen(nothing complex).

<UserControl x:Class="CustomizedPopup.UCPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ImageButton="clr-namespace:CustomizedPopup.ImageButton"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="853" d:DesignWidth="480">
 
<Grid x:Name="grdMain" Height="853" Width="480" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="20*"></RowDefinition>
<RowDefinition Height="60*"></RowDefinition>
<RowDefinition Height="20*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"></ColumnDefinition>
<ColumnDefinition Width="90*"></ColumnDefinition>
<ColumnDefinition Width="5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
 
<Grid x:Name="LayoutRoot" Grid.Row="1" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="01*"></ColumnDefinition>
<ColumnDefinition Width="98*"></ColumnDefinition>
<ColumnDefinition Width="01*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20*"></RowDefinition>
<RowDefinition Height="40*"></RowDefinition>
<RowDefinition Height="20*"></RowDefinition>
 
</Grid.RowDefinitions>
 
 
<Grid Grid.Column="1" Grid.Row="1" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="01*"></ColumnDefinition>
<ColumnDefinition Width="98*"></ColumnDefinition>
<ColumnDefinition Width="01*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10*"></RowDefinition>
<RowDefinition Height="80*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>
 
 
</Grid.RowDefinitions>
<Grid.Background>
<ImageBrush ImageSource="/Images/popup_box.png" />
</Grid.Background>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="40*"></RowDefinition>
<RowDefinition Height="20*"></RowDefinition>
<RowDefinition Height="40*"></RowDefinition>
 
</Grid.RowDefinitions>
<TextBlock x:Name="lblred" Grid.Row="0" Text="Customized Popup" VerticalAlignment="Center" Foreground="#FFFF2D2D" TextWrapping="Wrap" FontSize="30" FontWeight="Bold" HorizontalAlignment="Center" TextAlignment="Center" />
<TextBlock x:Name="lblwhite" Grid.Row="1" Text="Please select the action" HorizontalAlignment="Center" TextWrapping="Wrap" FontSize="25" TextAlignment="Center" ></TextBlock>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
<ColumnDefinition Width="8*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ImageButton:ImageButtonControl FontSize="25" x:Name="btnfirst" Grid.Column="1" Text="Ok" PressedSource="/Images/button_normal.png" Source="/Images/button_over.png" Tap="btnfirst_Tap" />
<ImageButton:ImageButtonControl FontSize="25" x:Name="btnsecond" Grid.Column="3" Text="Cancel" PressedSource="/Images/button_over.png" Source="/Images/button_normal.png" Tap="btnsecond_Tap"/>
</Grid>
 
 
</Grid>
 
 
</Grid>
 
 
 
 
</Grid>
</Grid>
 
</UserControl>

Added namespace for user control(imagebuttoncontrol) in headers section

<xmlns:ImageButton="clr-namespace:CustomizedPopup.ImageButton"


Designed the height and width of user control maximum(although made transparent) , so that that when pop up open no control in main screen clickable.

Below is code behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;
 
namespace CustomizedPopup
{
public partial class UCPopup : UserControl
{
public UCPopup()
{
InitializeComponent();
}
 
private void ClosePopup()
{
Popup Popclaim = this.Parent as Popup;
Popclaim.IsOpen = false;
 
}
 
private void btnsecond_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ClosePopup();
}
 
private void btnfirst_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ClosePopup();
}
 
}
}


Here comes the main screen :

On button tap opened pop up and user control as its child content

  private void btnpop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Testpopup = new System.Windows.Controls.Primitives.Popup();
 
Testpopup.Child =
new UCPopup
();
 
Testpopup.IsOpen = true;
Testpopup.HorizontalAlignment = HorizontalAlignment.Center;
Testpopup.VerticalAlignment = VerticalAlignment.Center;
 
Testpopup.VerticalOffset = 0;
}


On backKey press :

   protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
if ((Testpopup != null) && (Testpopup.IsOpen == true))
{
Testpopup.IsOpen = false;
e.Cancel = true;
}
}

Source code

You can download source code from here: File:CustomizedPopup.zip

This page was last modified on 9 June 2014, at 08:43.
539 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×