Example: Display a simple MessageDialog:
MessageDialog msg = new MessageDialog("The message displayed to the user.", "Title"); await msg.ShowAsync();
Example: Display a simple MessageDialog with the button OK instead of Close:
MessageDialog msg = new MessageDialog("The message displayed to the user.", "Title"); msg.Commands.Add(new UICommand("OK", command => { })); await msg.ShowAsync();
Example: Display a confirmation dialog:
bool result = await ShowConfirmationDialog(); Debug.WriteLine(result); // True or False ... public async Task<bool> ShowConfirmationDialog() { // Create the message dialog and set its content. var msg = new MessageDialog("Are you sure?", "Confirmation"); bool result = false; // Add commands and set their callbacks as inline event handlers. // The callback methods run in the UI thread. msg.Commands.Add(new UICommand("Yes", command => { result = true; })); msg.Commands.Add(new UICommand("No", command => { result = false; })); // Set the command that will be invoked by default i.e. when the user presses Enter. msg.DefaultCommandIndex = 0; // Set the command to be invoked when Esc is pressed. msg.CancelCommandIndex = 1; // Show the message dialog await msg.ShowAsync(); return result; }
Example: Display a dialog that returns command IDs:
MessageDialog msg = new MessageDialog("Choose a color", "Colors"); // Provide command IDs (the third parameter of the UICommand ctor). msg.Commands.Add(new UICommand("Red", null, Colors.Red)); msg.Commands.Add(new UICommand("Green", null, Colors.Green)); msg.Commands.Add(new UICommand("Blue", null, Colors.Blue)); // Obtain the returned command ID. IUICommand command = await msg.ShowAsync(); Color color = (Color)command.Id; // ... the same in one line. //Color color = (Color)(await msg.ShowAsync()).Id; Debug.WriteLine(color); // #FFFF0000 or #FF008000 or #FF0000FF
Example: Display a dialog and use its return value on UI. Note that we need to use a Dispatcher object to run code in the UI thread. Also, this example uses the Completed event handler.
MessageDialog msg = new MessageDialog("Choose a color", "Colors"); msg.Commands.Add(new UICommand("Red", null, Colors.Red)); msg.Commands.Add(new UICommand("Green", null, Colors.Green)); msg.Commands.Add(new UICommand("Blue", null, Colors.Blue)); // Show the MessageDialog. // Note that although the ShowAsync method returns to the application right away, // it does not return the object yet. IAsyncOperation<IUICommand> asyncOp = msg.ShowAsync(); // Set the Completed event handler and the Dispatcher callback as lambda expressions. // IMPORTANT: The Completed callback runs in a secondary thread, not the UI thread. asyncOp.Completed = (asyncInfo, asyncStatus) => { if (asyncStatus == AsyncStatus.Completed) { // Get the Color value. IUICommand command = asyncInfo.GetResults(); Color color = (Color)command.Id; // Use a Dispatcher to execute code in the UI thread. // IMPORTANT: It doesn't matter from which user interface element we obtain the CoreDispatcher. // All UI objects are created in the same UI thread, so they all work identically. IAsyncAction asyncAction = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // ... do something with the color on UI }); } };
Example: Display a dialog and wait five seconds for the user to make a selection. If no selection is made, dismiss the dialog automatically:
public sealed partial class MainPage : Page { // Keep asyncOp in a global field for cancellation. IAsyncOperation<IUICommand> asyncOp; public MainPage() { this.InitializeComponent(); } async void OnButtonClick(object sender, RoutedEventArgs args) { MessageDialog msg = new MessageDialog("Choose a color", "Colors"); msg.Commands.Add(new UICommand("Red", null, Colors.Red)); msg.Commands.Add(new UICommand("Green", null, Colors.Green)); msg.Commands.Add(new UICommand("Blue", null, Colors.Blue)); // Start a five-second timer. DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(5); timer.Tick += OnTimerTick; timer.Start(); // Show the MessageDialog. asyncOp = msg.ShowAsync(); IUICommand command = null; try { command = await asyncOp; } catch (TaskCanceledException exc) { Debug.WriteLine("TaskCanceledException: " + exc.Message); } catch (Exception exc) { Debug.WriteLine("Exception: " + exc.Message); } // Stop the timer timer.Stop(); // If the operation was cancelled, exit the method. if (command == null) return; // Get the Color value and set the background brush Color color = (Color)command.Id; Debug.WriteLine(color); } // If the MessageDialog is not dismissed in 5 seconds, // the Tick callback calls Cancel on the IAsyncOperation object // stored as the asyncOp field. void OnTimerTick(object sender, object args) { // Cancel the asynchronous operation. asyncOp.Cancel(); } }