Windows 8 utility tips series: 5. Windows 8 pop-up prompt box messagedialog and await, async keywords

Source: Internet
Author: User

In the previous pop-up prompts in Silverlight and WPF, The MessageBox class is displayed. In Windows 8, The messagedialog class under Windows. UI. popups namespace is used to replace MessageBox.

  MessagedialogClasses have the following common methods and attributes:

Showasync (): the asynchronous pop-up message box.

Commands: Add command. In the displayed dialog box, Add corresponding buttons synchronously.

Defaultcommandindex: sets the index of the default button. Press enter to activate the command button corresponding to the index.

Cancelcommandindex: sets the index for the cancel button. Press ESC to activate the command button for this index.

Title: The title of the pop-up message box

  Async: When used for method declaration, this keyword indicates that the compiler may have an await keyword in this method.

  Await: Used to simulate synchronous waits for asynchronous operations. An Asynchronous Operation with this keyword must continue to run after the asynchronous operation is completed, but it does not block the UI thread.

Note: The method body that uses the await keyword must use the async declaration method.

Now let's look at messagedialog, async, and await through an instance:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <Button Content="First Msg" HorizontalAlignment="Left"                Margin="430,196,0,0" VerticalAlignment="Top"                Height="51" Width="114" Click="First_Click"/>        <Button Content="Secend Msg" HorizontalAlignment="Left"            Margin="606,196,0,0" VerticalAlignment="Top"            Height="51" Width="114" Click="Secend_Click"/>        <Button Content="Third Msg" HorizontalAlignment="Left"            Margin="788,196,0,0" VerticalAlignment="Top"            Height="51" Width="114" Click="Third_Click"/>        <Button Content="Fourth Msg" HorizontalAlignment="Left"            Margin="975,196,0,0" VerticalAlignment="Top"            Height="51" Width="114" Click="Fourth_Click"/>        <TextBlock HorizontalAlignment="Left" Name="tbText"                   Margin="573,160,0,0" TextWrapping="Wrap"                    Text="TextBlock" VerticalAlignment="Top"                    Height="31" Width="565" FontSize="16"/>    </Grid>

I. Simplest messagedialog

Private async void first_click (Object sender, routedeventargs e) {messagedialog MSG = new messagedialog ("Hello world! This is the first prompt. "); MSG. Title =" prompt 1 "; var msginfo = await msg. showasync ();}

Ii. Message box for custom command set

Private async void secend_click (Object sender, routedeventargs e) {messagedialog msg1 = new messagedialog ("Hello world! This is the second prompt. "); msg1.title =" Tip 2 "; msg1.commands. add (New uicommand ("OK", command => {This. tbtext. TEXT = "you click OK, the second group prompts" ;})); msg1.commands. add (New uicommand ("cancel", command => {This. tbtext. TEXT = "you click the cancel button and the second group prompts" ;})); var msg1info = await msg1.showasync ();}

3. Use await to simulate synchronization to obtain the code segment that uses the command ID to run the response.

Private async void third_click (Object sender, routedeventargs e) {messagedialog msg1 = new messagedialog ("Hello world! This is the third prompt. "); msg1.title =" Tip 3 "; msg1.commands. add (New uicommand ("OK", null, 0); msg1.commands. add (New uicommand ("cancel", null, 1); msg1.defacommandcommandindex = 0; msg1.cancelcommandindex = 1; var msg1info = await msg1.showasync (); Switch (convert. toint32 (msg1info. ID) {Case 0: This. tbtext. TEXT = "you click OK, the third group prompts"; break; Case 1: This. tbtext. TEXT = "when you click the cancel button, the third group prompts"; break; default: break ;}}

4. Separate the command method body and write the method body.

Private async void fourth_click (Object sender, routedeventargs e) {messagedialog msg1 = new messagedialog ("Hello world! This is the fourth prompt. "); msg1.title =" Tip 3 "; msg1.commands. add (New uicommand ("OK", new uicommandinvokedhandler (this. showtextenter); msg1.commands. add (New uicommand ("cancel", new uicommandinvokedhandler (this. showtextcancel); msg1.defacommandcommandindex = 0; msg1.cancelcommandindex = 1; var msg1info = await msg1.showasync ();} private void showtextenter (iuicommand command) {This. tbtext. TEXT = "you click OK, the fourth group prompts";} private void showtextcancel (iuicommand command) {This. tbtext. TEXT = "you have clicked the cancel button and the fourth group prompts ";}

Finally, let's take a look at the running effect as shown in. For the source code, click win8message.zip to download it.

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.