private void CloseDialog(IUICommand command) { UnsubscribeEvents(); if (command != null && command.Invoked != null) { command.Invoked(command); } popup.IsOpen = false; taskCompletionSource.SetResult(command); }
private void Clicked(object sender, Android.Content.DialogClickEventArgs e) { if (Commands.Count > 0) { _selectedCommand = Commands[-1 - e.Which]; if (_selectedCommand.Invoked != null) { _selectedCommand.Invoked(_selectedCommand); } } handle.Set(); }
private void CloseDialog(IUICommand command) { UnsubscribeEvents(); if (command != null) { command.Invoked(command); } _popup.IsOpen = false; _taskCompletionSource.SetResult(command); }
/// <summary> /// Begins an asynchronous operation showing a dialog. /// </summary> /// <returns>An object that represents the asynchronous operation. /// For more on the async pattern, see Asynchronous programming in the Windows Runtime.</returns> /// <remarks>In some cases, such as when the dialog is closed by the system out of your control, your result can be an empty command. /// Returns either the command selected which destroyed the dialog, or an empty command. /// For example, a dialog hosted in a charms window will return an empty command if the charms window has been dismissed.</remarks> public Task <IUICommand> ShowAsync() { if (Commands.Count > MaxCommands) { throw new InvalidOperationException(); } #if __ANDROID__ Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity); Android.App.AlertDialog dialog = builder.Create(); dialog.SetTitle(Title); dialog.SetMessage(Content); if (Commands.Count == 0) { dialog.SetButton(-1, Resources.System.GetString(Android.Resource.String.Cancel), new EventHandler <Android.Content.DialogClickEventArgs>(Clicked)); } else { for (int i = 0; i < Commands.Count; i++) { dialog.SetButton(-1 - i, Commands[i].Label, new EventHandler <Android.Content.DialogClickEventArgs>(Clicked)); } } dialog.Show(); return(Task.Run <IUICommand>(() => { _handle.WaitOne(); return _selectedCommand; })); #elif __IOS__ || __TVOS__ uac = UIAlertController.Create(Title, Content, UIAlertControllerStyle.Alert); if (Commands.Count == 0) { uac.AddAction(UIAlertAction.Create("Close", UIAlertActionStyle.Cancel | UIAlertActionStyle.Default, ActionClicked)); } else { for (int i = 0; i < Commands.Count; i++) { UIAlertAction action = UIAlertAction.Create(Commands[i].Label, CancelCommandIndex == i ? UIAlertActionStyle.Cancel : UIAlertActionStyle.Default, ActionClicked); uac.AddAction(action); } } UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController; while (currentController.PresentedViewController != null) { currentController = currentController.PresentedViewController; } currentController.PresentViewController(uac, true, null); return(Task.Run <IUICommand>(() => { _handle.WaitOne(); return _selectedCommand; })); #elif __MAC__ NSAlert alert = new NSAlert(); alert.AlertStyle = NSAlertStyle.Informational; alert.InformativeText = Content; alert.MessageText = Title; foreach (IUICommand command in Commands) { var button = alert.AddButton(command.Label); } alert.BeginSheetForResponse(NSApplication.SharedApplication.MainWindow, NSAlert_onEnded); return(Task.Run <IUICommand>(() => { _handle.WaitOne(); return _selectedCommand; })); #elif WINDOWS_PHONE List <string> buttons = new List <string>(); foreach (IUICommand uic in Commands) { buttons.Add(uic.Label); } if (buttons.Count == 0) { buttons.Add("Close"); } MessageDialogAsyncOperation asyncOperation = new MessageDialogAsyncOperation(this); string contentText = Content; // trim message body to 255 chars if (contentText.Length > 255) { contentText = contentText.Substring(0, 255); } while (Microsoft.Xna.Framework.GamerServices.Guide.IsVisible) { Thread.Sleep(100); } Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox( string.IsNullOrEmpty(Title) ? " " : Title, contentText, buttons, DefaultCommandIndex == uint.MaxValue ? 0 : (int)DefaultCommandIndex, // can choose which button has the focus Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None, // can play sounds result => { int?returned = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result); // process and fire the required handler if (returned.HasValue) { if (Commands.Count > returned.Value) { IUICommand theCommand = Commands[returned.Value]; asyncOperation.SetResults(theCommand); if (theCommand.Invoked != null) { theCommand.Invoked(theCommand); } } else { asyncOperation.SetResults(null); } } else { asyncOperation.SetResults(null); } }, null); return(asyncOperation.AsTask <IUICommand>()); #elif WINDOWS_UWP if (Commands.Count < 3 && Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.UI.ApplicationSettings.ApplicationsSettingsContract", 1)) { Windows.UI.Xaml.Controls.ContentDialog cd = new Windows.UI.Xaml.Controls.ContentDialog(); cd.Title = Title; cd.Content = Content; if (Commands.Count == 0) { cd.PrimaryButtonText = "Close"; } else { cd.PrimaryButtonText = Commands[0].Label; cd.PrimaryButtonClick += Cd_PrimaryButtonClick; if (Commands.Count > 1) { cd.SecondaryButtonText = Commands[1].Label; cd.SecondaryButtonClick += Cd_SecondaryButtonClick; } } return(Task.Run <IUICommand>(async() => { ManualResetEvent mre = new ManualResetEvent(false); IUICommand command = null; await cd.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { ContentDialogResult dr = await cd.ShowAsync(); if (Commands.Count > 0) { switch (dr) { case ContentDialogResult.Primary: command = Commands[0]; if (Commands[0].Invoked != null) { Commands[0].Invoked.Invoke(Commands[0]); } break; case ContentDialogResult.Secondary: command = Commands[1]; if (Commands[1].Invoked != null) { Commands[1].Invoked.Invoke(Commands[1]); } break; } } }); mre.WaitOne(); return command; })); } else { Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(Content, Title); foreach (IUICommand command in Commands) { dialog.Commands.Add(new Windows.UI.Popups.UICommand(command.Label, (c) => { command.Invoked(command); }, command.Id)); } return(Task.Run <IUICommand>(async() => { Windows.UI.Popups.IUICommand command = await dialog.ShowAsync(); if (command != null) { int i = 0; foreach (Windows.UI.Popups.IUICommand c in dialog.Commands) { if (command == c) { break; } i++; } return Commands[i]; } return null; })); } #elif WIN32 return(Task.Run <IUICommand>(() => { IUICommand cmd = ShowTaskDialog(); if (cmd != null) { cmd.Invoked?.Invoke(cmd); } return cmd; })); #else throw new PlatformNotSupportedException(); #endif }
public async Task <IUICommand> ShowAsync(string message, string title, MessageVisualizerOptions visualizerOptions) { if (visualizerOptions == null) { visualizerOptions = new MessageVisualizerOptions(UICommand.Ok); } var popup = new Window { WindowStartupLocation = WindowStartupLocation.CenterScreen, SizeToContent = SizeToContent.WidthAndHeight, Background = SystemColors.ControlLightBrush, Foreground = SystemColors.ControlTextBrush, Title = title, MinWidth = 200, MinHeight = 100, }; StackPanel rootPanel = new StackPanel(); TextBlock messageText = new TextBlock { Text = message, Margin = new Thickness(20), TextWrapping = TextWrapping.Wrap, MaxWidth = SystemParameters.PrimaryScreenWidth / 2, MaxHeight = SystemParameters.FullPrimaryScreenWidth / 2, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; rootPanel.Children.Add(messageText); var commands = visualizerOptions.Commands; if (commands.Count == 0) { commands = new[] { UICommand.Ok } } ; IUICommand finalCommand = null; WrapPanel buttonPanel = new WrapPanel() { Margin = new Thickness(10), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; for (int index = 0; index < commands.Count; index++) { var oneCommand = commands[index]; IUICommand command = oneCommand; Button commandButton = new Button { Content = command.Label, Tag = command, MinWidth = 75, Margin = new Thickness(5), Padding = new Thickness(10, 5, 10, 5), IsDefault = index == visualizerOptions.DefaultCommandIndex, IsCancel = index == visualizerOptions.CancelCommandIndex, }; commandButton.Click += (s, e) => { if (command.Invoked != null) { command.Invoked(); } finalCommand = ((Button)s).Tag as IUICommand; popup.DialogResult = !((Button)s).IsCancel; }; buttonPanel.Children.Add(commandButton); } rootPanel.Children.Add(buttonPanel); popup.Content = rootPanel; await Task.Run( () => { bool?rc = null; Application.Current.Dispatcher.Invoke( () => { rc = popup.ShowDialog(); }); }); return(finalCommand); } }