UWP C# MessageDialog Class.

VoyTec 671 Reputation points
2022-11-06T19:42:08.373+00:00

I want to use MessageDialog Class with Yes/No function.
But how to make code that will know which on was pressed?
I mean I need a code for
if (MessageDialog Class == Yes) {...}
else {}.

Developer technologies Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-11-06T20:05:43.183+00:00

    According to documentation, you can use the MessageDialog or ContentDialog classes. For example:

    private async void button_Click( object sender, RoutedEventArgs e )  
    {  
        var md = new MessageDialog( content: "Are you sure?", title: "Warning" );  
      
        bool yes = false;  
      
        md.Commands.Add( new UICommand( "Yes", c => yes = true ) );  
        md.Commands.Add( new UICommand( "No", c => yes = false ) );  
      
        md.DefaultCommandIndex = 0;  
        md.CancelCommandIndex = 1;  
      
        await md.ShowAsync( );  
      
        if( yes )  
        {  
            // "Yes" pressed  
            // . . .  
        }  
        else  
        {  
            // . . .  
        }  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.