65.9K
CodeProject is changing. Read more.
Home

PopupMenu and Message DailogBox in Windows 8

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (2 votes)

Aug 26, 2014

CPOL
viewsIcon

8792

downloadIcon

89

PopupMenu and Message DailogBox in Windows 8

Introduction

I am going to explain how to create a popup menu and message dailogbox in Windows Store apps. When we open any application, there is a menu bar. When we click on any menu item, the popup menu will be shown from which we can select any popup menu item. In this tip, I explain how to create a Popup menu. The message box here is fully user-defined, we can add the commands to the message box and also add an action with it.

  1. Create a new blank app name Popupmenu_N_Msgdialogbox:

  2. Add Button and Popup control in MainPage.xaml:
            <Button Content="Show Popup" 
        Click="ShowPopup" Margin="120,69,0,0" VerticalAlignment="Top"/>
            <Popup  VerticalOffset="10" 
            HorizontalOffset="300" x:Name="StandardPopup" 
    IsLightDismissEnabled="True">
                <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" 
                BorderThickness="2" 
                            Background="{StaticResource ApplicationPageBackgroundThemeBrush}" 
                            Width="212" Height="200">
                    <StackPanel HorizontalAlignment="Center" 
                    VerticalAlignment="Center">
                        <TextBlock Text="Simple Popup" 
                        FontSize="24.667" HorizontalAlignment="Center"/>
                        <Button Content="Close" Click="ShowPopupClose" 
                        HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </Popup>
  3. And in MainPage.Xaml.cs file, add this code:
            private void ShowPopup(object sender, RoutedEventArgs e)
            {
                if (!StandardPopup.IsOpen) { StandardPopup.IsOpen = true; }
            }
    
            private void ShowPopupClose(object sender, RoutedEventArgs e)
            {
                if (StandardPopup.IsOpen) { StandardPopup.IsOpen = false; }
     }
  4. The output of this code is as follows:

  5. Add new Button for message dailogbox:
    <Button Content="MessageDialog Box" Click="MessageDialogBox" HorizontalAlignment="Left" Margin="120,176,0,0" 
                    VerticalAlignment="Top” Width="238"/>
  6. Add namespace in mainpage.xaml.cs file:

  7. And add this code in Mainpage.xaml.cs file:
    private async void MessageDialogBox(object sender, RoutedEventArgs e)
            {
                var messagedialog = new MessageDialog("No internet connection has been found.");
                messagedialog.Commands.Add(new UICommand("Try again"));
                messagedialog.Commands.Add(new UICommand("Close"));
                await messagedialog.ShowAsync();
     }
  8. The output of this code is as follows: