Skip to content

How to display a dialog when the application is closing? #8459

Answered by castorix
harvinders asked this question in Q&A
Discussion options

You must be logged in to vote

This works for me (Windows 10 22H2, Windows app SDK 1.1.0) :

    private async void AppW_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
    {
        args.Cancel = true;

        ContentDialog cd = new ContentDialog
        {
            Title = "Question",
            Content = "Do you want to quit ?",
            PrimaryButtonText = "Yes",
            SecondaryButtonText = "No"
        };
        cd.XamlRoot = this.Content.XamlRoot;
        var cdResult = await cd.ShowAsync();
        if (cdResult == ContentDialogResult.Primary)
        {
            this.Close();
        }
    }

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by harvinders
Comment options

You must be logged in to vote
1 reply
@DarranRowe
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants