We have had a look at the application bar and its working in last tutorial – so now in this part of the Windows Phone 7.5 Mango App development series, we shall learn how to create and display popup windows in Windows Phone 7.5 Application.
Sometimes it is easier to display a popup window than to navigate the user to a whole new XAML page for certain operations. So a popup window can be simply informative or can confirm the user’s actions like “Do you wish to delete this file?” The technique of creating a popup window is simple. All we have to do is create a Canvas and put some controls in it. We also have to define a mechanism to display and hide the popup window as per the need of the situation. So lets get started!
Create a new Widows Phone 7 Application Project. Copy and paste the following XAML code in the Content panel Grid of the MainPage.xaml using the XAML editor in the designer window.
Text=”Click the button below to close this popup dialog.”
Margin=”125,300″ Canvas.Left=”-16″ Canvas.Top=”-7″ />
With the code given above we have created a button called openButton. We have also defined a Canvas (myDialog) with a text block called instructionsTextBlock and a button called closeButton. Note that the visibility of the Canvas is set to Collapsed.
Next we define the functionality in each of the button events. Right click the openButton_Click and select Goto Event Handler. Copy and paste the following C# line of code in the openButton_Click() method which sets the visibility of the myDialog canvas to Visible.
myDialog.Visibility = System.Windows.Visibility.Visible;
Repeat the same process for the closeButton_Click event. Copy and paste the following C# line of code in the closeButton_Click() method which sets the visibility of the myDialog canvas to hidden.
myDialog.Visibility = System.Windows.Visibility.Hidden;
Run the project and click the Open button to display the pop up. Clicking the close button hides the canvas. So that’s all about creating the dialog box without actually creating a new page. See you in next lesson on InputScopes.