コード例 #1
0
        private void OnDisplayQuestion(IntPtr userdata, IntPtr dialogid, IntPtr title, IntPtr text, DialogQuestionType questiontype, IntPtr cancel, IntPtr action1, IntPtr action2)
        {
            string strTitle         = Utf8InteropStringConverter.Utf8InteropToString(title);
            string strText          = Utf8InteropStringConverter.Utf8InteropToString(text);
            string strCancelButton  = Utf8InteropStringConverter.Utf8InteropToString(cancel);
            string strAction1Button = Utf8InteropStringConverter.Utf8InteropToString(action1);
            string strAction2Button = Utf8InteropStringConverter.Utf8InteropToString(action2);
            var    cts = new CancellationTokenSource();

            this.openDialogsCancellationTokens.Add(dialogid, cts);
            Task.Run(() => this.currentDialogManager.DisplayQuestionAsync(userdata, dialogid, strTitle, strText, questiontype, strCancelButton, strAction1Button, strAction2Button, cts.Token))
            .ContinueWith((Action <Task <QuestionAction?> >)(task =>
            {
                if (task.IsCompleted && task.Result != null)
                {
                    this.myManager.PostAction(dialogid, (int)task.Result.Value);
                }
                else if (!task.IsCanceled)
                {
                    this.myManager.DismissDialog(dialogid);
                }

                this.openDialogsCancellationTokens.Remove(dialogid);
            }));
        }
コード例 #2
0
 public async Task <QuestionAction?> DisplayQuestionAsync(IntPtr userdata, IntPtr dialogId, string title, string text, DialogQuestionType questionType,
                                                          string cancelButton, string action1Button, string action2Button, CancellationToken cancellationToken)
 {
     return(Task.FromResult <QuestionAction?>(null));
 }
コード例 #3
0
        static void Question(IntPtr data, IntPtr dialogId, string title, string text, DialogQuestionType type,
                             string cancelText, string?firstActionText, string?secondActionText)
        {
            if (_question == null)
            {
                return;
            }

            var cts = new CancellationTokenSource();
            var dlg = new Dialog(new DialogId(dialogId));

            _cts[dialogId] = cts;
            _question(dlg, title, text, type, cancelText, firstActionText, secondActionText, cts.Token);
        }
コード例 #4
0
        /// <summary>
        /// Displays a question dialog.
        ///
        /// There are three ways you can exit from this method:
        /// - The user has clicked on one of the two action buttons. The task completes with a <see cref="QuestionAction"/>
        /// - The user has cancelled the question dialog. The task completes with a <c>null</c> result
        /// - The dialog has been closed by libvlc. The cancellationToken is cancelled. In that case, you must close your dialog and <c>throw new TaskCanceledException</c>
        /// </summary>
        /// <param name="userdata">The user data, as given to the <see cref="DialogsManagement.UseDialogManager" /> method.</param>
        /// <param name="dialogId">The dialog identifier, as given by LibVlc</param>
        /// <param name="title">The dialog title</param>
        /// <param name="text">The dialog message</param>
        /// <param name="questionType">The kind of question</param>
        /// <param name="cancelButton">The text that is displayed on the button that cancels</param>
        /// <param name="action1Button">The text that is displayed on the first action button (or <c>null</c> if this button is not displayed)</param>
        /// <param name="action2Button">The text that is displayed on the second action button (or <c>null</c> if this button is not displayed)</param>
        /// <param name="cancellationToken">The token that is cancelled when libvlc asks to cancel the dialog.</param>
        /// <exception cref="TaskCanceledException">When the cancellation token has been cancelled, and the dialog has been closed.</exception>
        /// <returns>The action selected by the user, or <c>null</c> if (s)he clicked on "cancel"</returns>
        public async Task <QuestionAction?> DisplayQuestionAsync(IntPtr userdata, IntPtr dialogId, string title, string text, DialogQuestionType questionType,
                                                                 string cancelButton, string action1Button, string action2Button, CancellationToken cancellationToken)
        {
            MessageDialogStyle dialogStyle;
            string             negativeButtonText    = null;
            string             affirmativeButtonText = null;
            string             auxiliaryAction1      = null;

            if (action1Button == null && action2Button == null)
            {
                // No button is specified, just a cancel button, but Mahapps has no "NegativeOnly" dialog, let's put cancel on the Ok button
                dialogStyle           = MessageDialogStyle.Affirmative;
                affirmativeButtonText = cancelButton;
            }
            else if (action1Button != null && action2Button != null)
            {
                dialogStyle           = MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary;
                affirmativeButtonText = action1Button;
                auxiliaryAction1      = action2Button;
                negativeButtonText    = cancelButton;
            }
            else if (action1Button != null)
            {
                dialogStyle           = MessageDialogStyle.AffirmativeAndNegative;
                affirmativeButtonText = action1Button;
                negativeButtonText    = cancelButton;
            }
            else
            {
                // Handle the case where action2 is specified but not action1 . I don't know if this is possible.
                dialogStyle           = MessageDialogStyle.AffirmativeAndNegative;
                affirmativeButtonText = action2Button;
                negativeButtonText    = cancelButton;
            }

            var result = await this.metroWindow.Dispatcher.InvokeAsync(() => this.metroWindow.ShowMessageAsync(title, text, dialogStyle, new MetroDialogSettings
            {
                CancellationToken        = cancellationToken,
                AffirmativeButtonText    = affirmativeButtonText,
                FirstAuxiliaryButtonText = auxiliaryAction1,
                NegativeButtonText       = negativeButtonText
            })).Task.Unwrap();

            // Case 1 : libvlc cancelled the dialog, throw as required
            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            // Case 2 : User clicked on "cancel"
            if (result == MessageDialogResult.Canceled || result == MessageDialogResult.Negative || dialogStyle == MessageDialogStyle.Affirmative)
            {
                return(null);
            }
            else if (result == MessageDialogResult.FirstAuxiliary)
            {
                return(QuestionAction.Action2);
            }
            else if (action1Button == null)
            {
                // Case where only action2 is specified, then the "affirmative" button is Action2
                return(QuestionAction.Action2);
            }
            else
            {
                return(QuestionAction.Action1);
            }
        }