Message Dialog in Windows 8 XAML Application (Windows Store)
Well you will not find it; if you are doing a Windows 8 XAML Application for Windows Store. Instead you have to work on MessageDialog.
The all new Message Dialog is included in Windows.UI.Popups assembly. MSDN Describes Message Dialog here.
Let’s show a Message Dialog which would display a Title, a message, and a OK button.
You need to add the following lines of code.
As you see in above code; we have an error. Well it says that the method should be async. We would do that; as follows.
Now we would try and our first Message Dialog would look like following.
Message Dialog with 2 – 3 Buttons
Message Dialog supports addition multiple buttons; up to maximum 3. We can add the buttons in following way. We need to add commands. The UICommand would take the Button Name (Label name), and the InvokeHandler.
After adding the above buttons, let’s run the Message Dialog. Well it looked as following.
What if you need to provide Default options? Well there are two default options you can choose. Such as:
- Default Command – Invoked when Enter key is pressed.
- Cancel Command – Invoked when Escape key is pressed.
We could add these options by providing the index of the command. In our case:
Let’s run the Message Dialog with default options for keyboard interactions. Your Message Dialog would look like following.
Till now we have tried Message Dialog with a title and the content, well the title is always optional.
Your title less Message Dialog would look like following.
Which button did I click?
Well to know that; command is provided with a property called “Label” which is of type string.
You could put a switch case to identify each button’s Label. Something like following.
As you dig more; you would find that Commands is a collection which is read-only. That means you could only be able to add to the collection.
As we did before; we added maximum 3 commands to the dialog.
But if you add more than 3 commands; there would be an exception. As follows:
Hope this article helps. Thanks for reading.
Source Code: Click Here
Try out my Codeplex Projects: