Menu > Project > Add new item > Visual C# > Content Dialog

We delete the following default ContentDialog options to remove them:

PrimaryButtonText=
SecondaryButtonText=
PrimaryButtonClick=
SecondaryButtonClick=

The xaml code
<ContentDialog
    x:Class="ecu_winiot.MenuPopup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ecu_winiot"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Name="DialogBox"
    Title="Menu">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,20" >
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="80" />
            <RowDefinition Height="80" />
            <RowDefinition Height="80" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.ColumnSpan="2" Text="Select Option" Margin="5" />
        <Button Grid.Row="1" Grid.Column="0" Content="Item 1" x:Name="ButtonMenuItem1" Click="ButtonMenuItem_Click" Margin="5,0" Width="100" HorizontalAlignment="Center" />
        <Button Grid.Row="1" Grid.Column="1" Content="Item 2" x:Name="ButtonMenuItem2" Click="ButtonMenuItem_Click" Margin="5,0" Width="100" HorizontalAlignment="Center" />
        <Button Grid.Row="2" Grid.Column="0" Content="Item 3" x:Name="ButtonMenuItem3" Click="ButtonMenuItem_Click" Margin="5,0" Width="100" HorizontalAlignment="Center" />
        <Button Grid.Row="2" Grid.Column="1" Content="Item 4" x:Name="ButtonMenuItem4" Click="ButtonMenuItem_Click" Margin="5,0" Width="100" HorizontalAlignment="Center" />
        <Button Grid.Row="3" Grid.Column="1" Content="Cancel" x:Name="ButtonCancel" Click="ButtonMenuItem_Click" Margin="5,0" Width="100" HorizontalAlignment="Center" />
    </Grid>
</ContentDialog>
The xmal.cs code
    public sealed partial class MenuPopup : ContentDialog
    {
		//--------------------------
		//----- PUBLIC DEFINES -----
		//--------------------------
        public enum DialogResult
        {
            MenuItem1,
            MenuItem2,
            MenuItem3,
            MenuItem4,
            Cancel,
            Unknown
        }

		//---------------------------
		//----- PRIVATE DEFINES -----
		//---------------------------
		//private 

		//--------------------------
		//----- PUBLIC OBJECTS -----
		//--------------------------
		public DialogResult Result;

		//---------------------------
		//----- PRIVATE OBJECTS -----
		//---------------------------
		//private 


		//*********************************
		//*********************************
		//********** CONSTRUCTOR **********
		//*********************************
		//*********************************
        public MenuPopup()
        {
            this.InitializeComponent();
            this.Result = DialogResult.Unknown;
        }

		//************************************
		//************************************
		//********** BUTTON PRESSED **********
		//************************************
		//************************************
		private void ButtonMenuItem_Click(object sender, RoutedEventArgs e)
		{
			var ClickedButton = sender as Button;
			switch (ClickedButton.Name)
			{
			case "ButtonMenuItem1":
				this.Result = DialogResult.MenuItem1;
				break;
			case "ButtonMenuItem2":
				this.Result = DialogResult.MenuItem2;
				break;
			case "ButtonMenuItem3":
				this.Result = DialogResult.MenuItem3;
				break;
			case "ButtonMenuItem4":
				this.Result = DialogResult.MenuItem4;
				break;
			case "ButtonCancel":
				this.Result = DialogResult.Cancel;
				break;
			}
            
			//CLOSE DIALOG BOX
			DialogBox.Hide();
		}
	}
Using the dialog box

Note – make the _Click method “private async” as it will be doing an async call

    //*********************************
    //*********************************
    //********** MENU BUTTON **********
    //*********************************
    //*********************************
    private async void ButtonMenu_Click(object sender, RoutedEventArgs e)
    {

        //----- SHOW MENU DIALOG BOX -----
        MenuPopup DialogBox = new MenuPopup();
        await DialogBox.ShowAsync();
        if (DialogBox.Result == MenuPopup.DialogResult.MenuItem1)
        {
            //Do something...
            int i = 0;
        }
    }
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *