Ejemplo n.º 1
0
        /// <summary>
        /// Creates a MsgBoxDialog inside of the current window.
        /// </summary>
        /// <param name="window">The MetroWindow</param>
        /// <param name="title">The title of the MessageDialog.</param>
        /// <param name="message">The message contained within the MessageDialog.</param>
        /// <param name="style">The type of buttons to use.</param>
        /// <param name="settings">Optional settings that override the global metro dialog settings.</param>
        /// <returns>A task promising the result of which button was pressed.</returns>
        public Task <MsgBoxResult> ShowMsgBoxAsync(
            IMetroWindow metroWindow
            , IMsgBoxDialogFrame <MsgBoxResult> dialog
            , IMetroDialogFrameSettings settings = null)
        {
            metroWindow.Dispatcher.VerifyAccess();
            return(this.HandleOverlayOnShow(metroWindow, settings).ContinueWith(z =>
            {
                return (Task <MsgBoxResult>)metroWindow.Dispatcher.Invoke(new Func <Task <MsgBoxResult> >(() =>
                {
                    settings = settings ?? new MetroDialogFrameSettings();

                    SizeChangedEventHandler sizeHandler = this.SetupAndOpenDialog(metroWindow, dialog);
                    dialog.SizeChangedHandler = sizeHandler;

                    // Call this method in the dialog to wait until the dialog is closing ...
                    return dialog.WaitForLoadAsync().ContinueWith(x =>
                    {
                        if (DialogOpened != null)
                        {
                            metroWindow.Dispatcher.BeginInvoke(new Action(() => DialogOpened(this, new DialogStateChangedEventArgs())));
                        }

                        return dialog.WaitForButtonPressAsync().ContinueWith(y =>
                        {
                            //once a button as been clicked, begin removing the dialog.

                            dialog.OnClose();

                            if (DialogClosed != null)
                            {
                                metroWindow.Dispatcher.BeginInvoke(new Action(() => DialogClosed(this, new DialogStateChangedEventArgs())));
                            }

                            Task closingTask = (Task)metroWindow.Dispatcher.Invoke(new Func <Task>(() => dialog._WaitForCloseAsync()));
                            return closingTask.ContinueWith(a =>
                            {
                                return ((Task)metroWindow.Dispatcher.Invoke(new Func <Task>(() =>
                                {
                                    metroWindow.SizeChanged -= sizeHandler;

                                    this.RemoveDialog(metroWindow, dialog);

                                    return this.HandleOverlayOnHide(metroWindow, settings);
                                }))).ContinueWith(y3 => y).Unwrap();
                            });
                        }).Unwrap();
                    }).Unwrap().Unwrap();
                }));
            }).Unwrap());
        }