New Fashion Windows 8 Development (5): Message Box

Source: Internet
Author: User
Tags blank page

Old week's blog column: http://blog.csdn.net/tcjiaan

For more information, see the original author and source.

 

 

In the Windows 8 modern interface application, we are no longer familiar with MessageBox. What should we do if we need a prompt box?

Therefore, let's first get to know a class-messagedialog (located in the windows. UI. popups namespace ). Yes. You can guess seven or eight times by name. We just want to use this class to display the pop-up dialog box.

Step 1: first create a messagedialog instance;

Step 2: add several uicommands to the commands set, at least one, up to three. If you are bold, try adding four. This uicommand is the button displayed in the dialog box. Just like the message boxes in traditional windows, there may be at least one button or two or three buttons.

Step 4: display the message box.

 

Simply reading the text is a bit abstract, so let's use an example to illustrate it.

1. Start Vs and create a blank page application.

2. Three buttons are declared in mainpage. XAML to display the pop-up boxes with one, two, and three buttons respectively.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel verticalignment = "center"> <stackpanel orientation = "horizontal"> <button content = "message box of a button" Click = ""onokclick"/> <button content = "message box of the two buttons" Click = "onyesnoclick"/> <button content = "message box of the three buttons" Click = "onthirdclick" /> </stackpanel> <textblock X: name = "tbtip" margin = "," fontsize = "24"/> </stackpanel> </GRID>

 

3. Right-click the Click Event of the three buttons, and select "locate to event handler" from the pop-up menu. In this way, the event processing method is automatically generated in the code.

4. Open the code file and introduce the following namespace.

using Windows.UI.Popups;

5. process the preceding three click events.

Private async void onokclick (Object sender, routedeventargs e) {messagedialog MSG = new messagedialog ("message box with only one button. "," Prompt "); MSG. commands. add (New uicommand ("OK", new uicommandinvokedhandler (onuicommand); await MSG. showasync ();} private async void onyesnoclick (Object sender, routedeventargs e) {messagedialog MSG = new messagedialog ("message box with two buttons. "); MSG. commands. add (New uicommand ("yes", new uicommandinvokedhandler (onuicommand); MSG. commands. add (New uicommand ("no", new uicommandinvokedhandler (onuicommand); await MSG. showasync ();} private async void onthirdclick (Object sender, routedeventargs e) {messagedialog MSG = new messagedialog ("message box with three buttons. "); MSG. commands. add (New uicommand ("retry", new uicommandinvokedhandler (onuicommand); MSG. commands. add (New uicommand ("Ignore", new uicommandinvokedhandler (onuicommand); MSG. commands. add (New uicommand ("cancel", new uicommandinvokedhandler (onuicommand); // default button index MSG. cancelcommandindex = 2; MSG. defacommandcommandindex = 0; await MSG. showasync ();} async void onuicommand (iuicommand cmd) {await dispatcher. runa Sync (Windows. UI. Core. coredispatcherpriority. Normal, () => {This. tbtip. Text = "you clicked" + cmd. Label +. ";});}

 

The following constructor is used to instantiate a uicommand.

public UICommand(string label, UICommandInvokedHandler action);

Specify a method to bind to the uicommandinvokedhandler delegate. When a user clicks a uicommand, the corresponding method bound to the uicommandinvokedhandler is called. In this example, all uicommands are bound to the same method.

 

In addition, messagedialog has two attributes:

1. cancelcommandindex: The index of the "cancel" button by default. This index corresponds to the index of the uicommand added in commands, starting from 0 and in the order of adding, the index of the first uicommand is 0, the index of the second uicommand is 1, the index of the third is 2, and so on (of course, there are only three at most, index 2 ). If the cancelcommandindex attribute is set to 1, the second button in the message box is the default "cancel" command, which can be triggered by pressing the ESC key.

2. defacommandcommandindex: The index of the "OK" command by default. For example, if it is set to 0, the first button in commands is the default command, which can be triggered by pressing the Enter key.

 

To display messagedialog, call the showasync method. Note that this method is an asynchronous method, which uses the await keyword (C #5.0). At the same time, all methods that call the Asynchronous Method and add the await keyword, add the async keyword to the definition.

Now we can run the application.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.