1

How can I dismiss a Message dialog programmatically in Windows Phone 8.1. I created the dialog using showAsync(). If this is not possible which is the best method to create a custom message dialog with the following properties:

1. It can show test and hold buttons for user interaction.
2. It can be dismissed programmatically
3. Should block the view as a Normal MessageDialog do

1 Answers1

7

Use a ContentDialog rather than a MessageDialog. This will also allow customizing the dialog so you don't need to write a custom control unless you want to do something really crazy.

On Windows the MessageDialog is cancellable, but not on Windows Phone:

// Cancel the MessageDialog after 3 seconds on Windows
private async void Button_Click(object sender, RoutedEventArgs e)
{
    MessageDialog md = new MessageDialog("Lorem ipsum dolor sit amet","Message Dialog Title");
    var t = md.ShowAsync();

    await Task.Delay(TimeSpan.FromSeconds(3));

    // Ignored by the Windows Phone MessageDialog
    t.Cancel();
}

You can cancel ContentDialog with similar code on Windows Phone. You can use Visual Studio's ContentDialog template to create a custom ContentDialog if needed.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    ContentDialog cd = new ContentDialog();
    cd.Title = "Content Dialog";
    cd.PrimaryButtonText = "Close";
    cd.Content = "Lorem ipsum dolor sit amet";
    var t = cd.ShowAsync();

    await Task.Delay(TimeSpan.FromSeconds(3));

    t.Cancel();
}
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • Hi Rob, ContentDialog is working. But can I hold the ContentDialog when I press home key and then relaunch the application – Jose John Thottungal Mar 02 '15 at 06:47
  • I would also like to know how to set a eventHandler for the primary button from C# – Jose John Thottungal Mar 02 '15 at 07:20
  • You can prevent the dialog from closing by handling its Closing event and setting the Cancel property. Otherwise it will close when navigating away from the app. To handle the primary button handle the PrimaryButtonClick event or the PrimaryButtonCommand command – Rob Caplan - MSFT Mar 02 '15 at 07:39
  • Cancel property is for the Task or for the ContentDialog ? – Jose John Thottungal Mar 02 '15 at 09:02
  • Cancel is for the Task returned from ContentDialog.ShowAsync. It's a standard Task option which Task implementers (such as ContentDialog and MessageDialog) may or may not opt into. CD implements it. MD doesn't. – Rob Caplan - MSFT Mar 02 '15 at 16:12