/// <inheritdoc /> public override void Show(string caption, object content, ButtonConfig[] buttonConfigs, bool isFullScreen = false) { Guard.ArgumentNotNull(() => buttonConfigs); ButtonConfig positiveButton = null; ButtonConfig negativeButton = null; ButtonConfig neutralButton = null; if (buttonConfigs.Length < this.MinNumberOfButtons || buttonConfigs.Length > this.MaxNumberOfButtons) { throw new ArgumentException(string.Format("'buttonConfigs' supports a minimum of {0} and a maximum of {1} buttons", this.MinNumberOfButtons, this.MaxNumberOfButtons)); } var currentActivity = GetActivity(); var alertBuilder = new AlertDialog.Builder(currentActivity); alertBuilder.SetTitle(caption); alertBuilder.SetCancelable(isFullScreen); var stringContent = content as string; if (stringContent != null) { alertBuilder.SetMessage(stringContent); } var viewContent = content as View; if (viewContent != null) { alertBuilder.SetView(viewContent); } AlertDialog alertDialog = null; if (buttonConfigs.Length >= 1) { positiveButton = buttonConfigs[0]; alertBuilder.SetPositiveButton(positiveButton.Text, (senderAlert, args) => { positiveButton.Action(); }); positiveButton.EnabledChanged += (sender, isEnabled) => { UpdateEnabledChanged(alertDialog, DialogButtonType.Positive, isEnabled); }; } if (buttonConfigs.Length >= 2) { negativeButton = buttonConfigs[1]; alertBuilder.SetNegativeButton(negativeButton.Text, (senderAlert, args) => { negativeButton.Action(); }); negativeButton.EnabledChanged += (sender, isEnabled) => { UpdateEnabledChanged(alertDialog, DialogButtonType.Negative, isEnabled); }; } if (buttonConfigs.Length == 3) { neutralButton = buttonConfigs[2]; alertBuilder.SetNeutralButton(neutralButton.Text, (senderAlert, args) => { neutralButton.Action(); }); neutralButton.EnabledChanged += (sender, isEnabled) => { UpdateEnabledChanged(alertDialog, DialogButtonType.Neutral, isEnabled); }; } // dispatch the alert to the UI thread this.dispatcherService.CheckBeginInvokeOnUI( () => { alertDialog = alertBuilder.Create(); alertDialog.Show(); if (positiveButton != null) { UpdateEnabledChanged(alertDialog, DialogButtonType.Positive, positiveButton.IsEnabled); } if (negativeButton != null) { UpdateEnabledChanged(alertDialog, DialogButtonType.Negative, negativeButton.IsEnabled); } if (neutralButton != null) { UpdateEnabledChanged(alertDialog, DialogButtonType.Neutral, neutralButton.IsEnabled); } }); }
/// <inheritdoc /> public override void Show(string caption, object content, ButtonConfig[] buttonConfigs, bool isFullScreen = false) { Guard.ArgumentNotNull(() => buttonConfigs); ButtonConfig primaryButton = null; ButtonConfig secondaryButton = null; if (buttonConfigs.Length < this.MinNumberOfButtons || buttonConfigs.Length > this.MaxNumberOfButtons) { throw new ArgumentException(string.Format("'buttonConfigs' supports a minimum of {0} and a maximum of {1} buttons", this.MinNumberOfButtons, this.MaxNumberOfButtons)); } if (buttonConfigs.Length >= 1) { primaryButton = buttonConfigs[0]; } if (buttonConfigs.Length == 2) { secondaryButton = buttonConfigs[1]; } //http://www.reflectionit.nl/blog/2015/windows-10-xaml-tips-messagedialog-and-contentdialog //http://www.kunal-chowdhury.com/2013/02/win8dev-tutorial-windows-store-winrt-messagedialog.html //https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.contentdialog.aspx //ContentDialog contentDialog = new ContentDialog(); == TODO CHeck for examples var stringContent = content as string; if (stringContent != null) { var messageDialog = new MessageDialog(stringContent, caption); if (primaryButton != null) { messageDialog.Commands.Add(new UICommand(primaryButton.Text, delegate { primaryButton.Action(); })); } if (secondaryButton != null) { messageDialog.Commands.Add(new UICommand(secondaryButton.Text, delegate { secondaryButton.Action(); })); } messageDialog.ShowAsync(); } else { var contentDialog = new ContentDialog { Title = caption, //RequestedTheme = ElementTheme.Dark, #if WINDOWS_UWP FullSizeDesired = isFullScreen, #endif //MaxWidth = this.ActualWidth // Required for Mobile! }; var cp = new ContentPresenter(); cp.Content = content; #if WINDOWS_APP contentDialog.ContentWrapper = cp; #else contentDialog.Content = cp; #endif EventHandler <bool> primaryButtonOnEnabledChanged = null; if (primaryButton != null) { primaryButtonOnEnabledChanged = (sender, isEnabled) => { contentDialog.IsPrimaryButtonEnabled = isEnabled; }; primaryButton.EnabledChanged += primaryButtonOnEnabledChanged; contentDialog.PrimaryButtonText = primaryButton.Text; contentDialog.IsPrimaryButtonEnabled = primaryButton.IsEnabled; contentDialog.PrimaryButtonClick += delegate { primaryButton.Action(); }; } EventHandler <bool> secondaryButtonOnEnabledChanged = null; if (secondaryButton != null) { secondaryButtonOnEnabledChanged = (sender, isEnabled) => { contentDialog.IsSecondaryButtonEnabled = isEnabled; }; secondaryButton.EnabledChanged += secondaryButtonOnEnabledChanged; contentDialog.SecondaryButtonText = secondaryButton.Text; contentDialog.IsSecondaryButtonEnabled = secondaryButton.IsEnabled; contentDialog.SecondaryButtonClick += delegate { secondaryButton.Action(); }; } #if WINDOWS_APP // Windows Store Apps ContentDialog does not return an async task // when calling ShowAsync(). TaskCompletionSource comes to the rescue! var tcs = new TaskCompletionSource <object>(); contentDialog.Closed += (sender, args) => { tcs.TrySetResult(null); }; contentDialog.ShowAsync(); var unsubscribeTask = tcs.Task; #else var unsubscribeTask = contentDialog.ShowAsync().AsTask(); #endif unsubscribeTask.ContinueWith( ct => { if (primaryButton != null && primaryButtonOnEnabledChanged != null) { primaryButton.EnabledChanged -= primaryButtonOnEnabledChanged; } if (secondaryButton != null && secondaryButtonOnEnabledChanged != null) { secondaryButton.EnabledChanged -= secondaryButtonOnEnabledChanged; } }); } }
/// <inheritdoc /> public override void Show(string caption, object content, ButtonConfig[] buttonConfigs, bool isFullScreen = false) { Guard.ArgumentNotNull(() => buttonConfigs); ButtonConfig leftButton = null; ButtonConfig rightButton = null; if (buttonConfigs.Length < this.MinNumberOfButtons || buttonConfigs.Length > this.MaxNumberOfButtons) { throw new ArgumentException(string.Format("'buttonConfigs' supports a minimum of {0} and a maximum of {1} buttons", this.MinNumberOfButtons, this.MaxNumberOfButtons)); } var messageBox = new CustomMessageBox { Caption = caption, IsFullScreen = isFullScreen }; if (buttonConfigs.Length >= 1) { leftButton = buttonConfigs[0]; messageBox.LeftButtonContent = leftButton.Text; } if (buttonConfigs.Length == 2) { rightButton = buttonConfigs[1]; messageBox.RightButtonContent = rightButton.Text; } var stringContent = content as string; if (stringContent != null) { messageBox.Message = stringContent; messageBox.Content = null; } else { ////var hyperlinkButton = content as HyperlinkButton; ////if (hyperlinkButton != null) ////{ //// TiltEffect.SetIsTiltEnabled(hyperlinkButton, true); ////} messageBox.Message = null; messageBox.Content = content; } messageBox.Dismissed += (s1, e1) => { switch (e1.Result) { case CustomMessageBoxResult.LeftButton: if (leftButton != null) { leftButton.Action(); } break; case CustomMessageBoxResult.RightButton: if (rightButton != null) { rightButton.Action(); } break; case CustomMessageBoxResult.None: break; default: break; } messageBox.Content = null; // This is to avoid ArgumentException 'Value does not fall within the expected range' }; Deployment.Current.Dispatcher.BeginInvoke(() => { messageBox.Show(); }); }