MessageBox Popup in Windows Phone 8.1

Introduction

Yesterday, when I was working on an application for Windows Phone 8.1, I was stuck in a place where I wanted to prompt the default MessageBox in my application since it has already been there in Windows Phone 7 or 8. But I was shocked at the time when I typed the so-called MessaBox.Show(); and it showed the following message : “The name ‘MessageBox’ doesn't exist in the current context”. As I was not familiar with the Windows Store (Windows 8.1) application development I was a little bit confused. I searched for the solution and finally I came to understand that since Windows Phone 8.1 follows the similar concept as Windows Store, the MessageBox is replaced with something different and it is a such a trivial concept that hardly anyone bothers about it.

So I decided to write an article on how to show a MessageBox prompt in Windows Phone 8.1.

Step 1

To build a Windows Phone 8.1 application, ensure you have Visual Studio 2013 and the Windows Phone 8.1 SDK installed in your system.

Go to "New Project". Under "Installed" > "Templates" > "Visual C#" > "Store Apps" select "Windows Phone Apps" then select "Blank App (Windows Phone)" and provide it a name as you choose (here I am using "MessageBoxWP8.1").

Step 2

Navigate to the "MainPage.xaml" section of the project and add a "Button" Control that helps to pop-up the MessageBox whenever we click on it.

  1. <Grid>  
  2.     <Button x:Name="MessageBoxDisplay" Content="Show MessageBox" FontSize="32" Margin="55,304,0,230" Height="106" Click="MessageBoxDisplay_Click"/>  
  3. </Grid> 

Your MainPage will be like:

Main Page in Windows Phone

Step 3

Now navigate to the Code section of the project. In MainPage.xaml.cs add the following directive:

  1. using Windows.UI.Popups; 

Step 4

In the Button Event handler add the following line of code:

  1. private async void MessageBoxDisplay_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    //Creating instance for the MessageDialog Class  
  4.    //and passing the message in it's Constructor  
  5.    MessageDialog msgbox = new MessageDialog("Message Box is displayed");  
  6.    //Calling the Show method of MessageDialog class  
  7.    //which will show the MessageBox  
  8.    await msgbox.ShowAsync();  

That's all; just compile and run your project. Whenever you press the button in the project you will see the MessageBox displayed with the message that we passed in the constructor.

Message Box in Windows Phone

If you have any query then feel free to ask. I am including the source code also.

Thanks.


Similar Articles