axbeit
axbeit

Reputation: 883

How to make a simple ContentDialog in .xaml?

What would it take to create a ContentDialog?

I basically have a button. When I tap that button the ContentDialog opens. The ContentDialog shall include a text like "this is an example" and 2 Buttons. How does the <ConTentDialog></ContentDialog> have to look like? Example:

<Page
    x:Class="PDFViewerSDK_Win10.PDFReaderPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PDFViewerSDK_Win10"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" SizeChanged="OnSizeChanged">

    <ContentDialog x:Name="test" PrimaryButtonText="Ok" SecondaryButtonText="Cancel" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" >
        <Image Source="Assets/images/icon_menu.png"/>
    </ContentDialog>
</Page>

Upvotes: 2

Views: 2050

Answers (1)

CStruggle
CStruggle

Reputation: 173

A content dialog can be easily created in your code-behind. You can then have that C# code run on the click event for the button you mentioned you wanted.

Your other option is to create a content dialog on XAML, as you already posted in your original question, so that when the button is clicked, you just refer to the content dialog on your code-behind and call the ShowAsync() method.

I could write example code in this response, but in the interest of this question being relevant to visitors in the future I encourage you to refer to Microsoft's documentation which will be better maintained. I already read it and it does have specific examples that will be of great help to you.

Edit: Posting sample code as a reference.

MainPage.XAML:

<Grid>
    <Button 
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Height="100" Width="250"
        Content="Show Content"
        FontSize="30px"
        x:Name="ContentBtn"
        Click="ContentBtn_Click"/>

    <ContentDialog x:Name="ContentDialog"
        Title="This is an example"
        PrimaryButtonText="Ok"
        CloseButtonText="Cancel"
        DefaultButton="Primary">
    </ContentDialog>
</Grid>

MainPage.XAML.cs:

private async void ContentBtn_Click(object sender, RoutedEventArgs e) {
        await ContentDialog.ShowAsync(); 
    }

Upvotes: 2

Related Questions