Over a million developers have joined DZone.

Windows Phone 7 and Message Box

DZone's Guide to

Windows Phone 7 and Message Box

· Mobile Zone
Free Resource

In this article we will discuss about the MessageBox in Windows Phone 7. We will also discuss on how to customize the MessageBox in Windows Phone 7. We will use Microsoft.Xna.Framework.GamerServices to create customize MessageBox in Windows Phone 7.

Let's write some code:

Step 1: Create a Windows Phone Application project.

Windows Phone 7 - MessageBox

Step 2: Let's add three buttons in the MainPage.xaml. First two buttons will be used to demonstrate normal message box. First message box will have only OK button and second message box will have OK and Cancel both.

The third button will be used to demonstrate customized message box using Microsoft.Xna.Framework.GamerServices.

The textbox will be used to display which button of messagge box has been clicked.

<Button Content="Ok Demo" Height="72" HorizontalAlignment="Center"
Margin="0,0,0,0" Name="Ok" VerticalAlignment="Top" Width="160" Click="Ok_Click" />

<Button Content="Ok Cancel Demo" Height="72" HorizontalAlignment="Center"
Margin="0,75,0,0" Name="OkCancel" VerticalAlignment="Top" Width="260" Click="OkCancel_Click" />

<Button Content="XNA MessageBox Demo" Height="72" HorizontalAlignment="Center"
Margin="0,150,0,0" Name="XNAMessageBox" VerticalAlignment="Top" Width="340" Click="XNAMessageBox_Click" />

<TextBlock x:Name="MessageResult" Text="" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Center" Margin="0,170,0,0" Height="40"/>


Step 3: Add reference of Microsoft.Xna.Framework.GamerServices


Windows Phone 7 - XNA

Step 4: Add below code for first two buttons to get OK and OK and Cancel message box.


private void Ok_Click(object sender, RoutedEventArgs e)
   MessageBoxResult res = MessageBox.Show("OK", "First Message Box", MessageBoxButton.OK);
   if (res == MessageBoxResult.OK)
      MessageResult.Text = "Ok Selected";

private void OkCancel_Click(object sender, RoutedEventArgs e)
   MessageBoxResult res = MessageBox.Show("OK and Cancel", "First Message Box", MessageBoxButton.OKCancel);
   if (res == MessageBoxResult.OK)
      MessageResult.Text = "Ok Selected";
   if (res == MessageBoxResult.Cancel)
      MessageResult.Text = "Cancel Selected";

 MessageBoxButton has only two property OK and OKCancel


Windows Phone 7 - OK Message Box

MessageBoxResult has Cancel, No, None, OK and Yes.


Windows Phone 7 - Message Result

Now MessageBoxButton has only OK and OKCancel option but MessageBoxResult has Cancel, No, None, OK and Yes option. As there is no Yes and No is present in MessageBoxButton so there is no use of Yes and No option of MessageBoxResult. Infact None option of MessageBoxResult also doesn't have any significance.

Now let's add code for the third button.

private void XNAMessageBox_Click(object sender, RoutedEventArgs e)
   //0 specifies index of focus of button
   Guide.BeginShowMessageBox("XNA MessageBox Demo", "This is XNA Message Box", new string[] { "Custom OK", "Custom Cancel" }, 0,MessageBoxIcon.Warning, new AsyncCallback(OnMessageBoxAction), null);

Signture of Guide.BeginShowMessageBox

The button name can be customized using Microsoft.Xna.Framework.GamerServices. I have used the MessageBox button caption as Custom OK and Custom Cancel.

MessageBoxIcon button has Alert, Error, None and Warning.

Windows Phone 7 - MessageBoxIcon

None won't generate any sound, I got same sound for rest of three Alert, Error and Warning.

To perform any action based on the option selected the asynchronous method needs to be called.

private void OnMessageBoxAction(IAsyncResult ar)
   int? selectedButton = Guide.EndShowMessageBox(ar);
   switch (selectedButton)
      case 0:
         Deployment.Current.Dispatcher.BeginInvoke(() => MessageResult.Text = "Custom Ok Selected" );

      case 1:
         Deployment.Current.Dispatcher.BeginInvoke(() => MessageResult.Text = "Custom Cancel Selected");


Step 5: Now run the application.

  Windows Phone 7 - MessageBox

You will get below different kind of message box on click of any of the above button.

Windows Phone 7 - MessageBox OK Demo Windows Phone 7 - MessageBox OKCancelDemo Windows Phone 7 - MessageBox Custom OKCancelDemo

This ends the article of Windows Phone 7 message box.


Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}