Windows Phone 7 and Message Box
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.

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

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

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

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.
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" );
break;
case 1:
Deployment.Current.Dispatcher.BeginInvoke(() => MessageResult.Text = "Custom Cancel Selected");
break;
default:
break;
}
}Step 5: Now run the application.
You will get below different kind of message box on click of any of the above button.
This ends the article of Windows Phone 7 message box.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





