Beispiel #1
0
        /// <summary>
        /// Shows the window as a modal dialog
        /// </summary>
        public DialogResult ShowDialog()
        {
            this.Parent  = WindowElement.ModalContainerPanel;
            this.isModal = true;

            this.LockKeyboardNavigation();
            this.LockMouseOutside();

            this.Show();

            // Set DialogResult default value
            this.DialogResult = DialogResult.None;

            try
            {
                // Push the current thread to a modal state
                ComponentDispatcher.PushModal();

                // Create a DispatcherFrame instance and use it to start a message loop
                this.dispatcherFrame = new DispatcherFrame();
                Dispatcher.PushFrame(this.dispatcherFrame);
            }
            finally
            {
                // Pop the current thread from modal state
                ComponentDispatcher.PopModal();
            }

            return(this.DialogResult);
        }
Beispiel #2
0
 private static TResult PushFrame <TResult>(IDispatcherService dispatcher, Func <Task <TResult> > task)
 {
     return(dispatcher.Invoke(() =>
     {
         var frame = new DispatcherFrame();
         var frameTask = task().ContinueWith(x => { frame.Continue = false; return x.Result; });
         ComponentDispatcher.PushModal();
         Dispatcher.PushFrame(frame);
         ComponentDispatcher.PopModal();
         return frameTask.Result;
     }));
 }
Beispiel #3
0
 public static void Enter(ModalStateCancellationToken cancellationToken)
 {
     try
     {
         ComponentDispatcher.PushModal();
         Dispatcher.PushFrame(cancellationToken.DispatcherFrame);
     }
     finally
     {
         ComponentDispatcher.PopModal();
     }
 }
        public ModalResult ShowModal(PopupItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (this.VerifyIsProcessDialogBox(item))
            {
                throw new InvalidOperationException($"视图类型 {typeof(ProcessDialogBox)} 不可以模态方式进行显示。");
            }

            //以模态显示窗口时,若发现最上层是模态窗口
            //当顶级窗口为MessageDialogBox时不予显示当前窗口
            //当顶级窗口不是MessageDialogBox时,若当前为MessageDialogBox,则显示
            if (this.VerifyTopItemModal())
            {
                if (this.VerifyTopItemIsMessageDialogBox() ||
                    !this.VerifyIsMessageDialogBox(item))
                {
                    PopupItemContainer container = this.PopupContainerFromIndex(this._popupStack.Items.Count - 1);
                    if (container != null)
                    {
                        container.Flicker();
                    }

                    return(null);
                }
            }

            this.VerifyCanShow(item);

            item._isShowing      = true;
            item._showingAsModal = true;
            item._modalResult    = null;
            CancelEventArgs ce = null;

            try
            {
                item.InternalShowing(out ce);
            }
            catch (Exception)
            {
                //状态还原
                item._isShowing      = false;
                item._showingAsModal = false;
                throw;
            }

            bool notcanceled = !ce.Cancel;

            try
            {
                if (notcanceled)
                {
                    ComponentDispatcher.PushModal();
                    item._dispatcherFrame = new DispatcherFrame();
                    try
                    {
                        this.ShowCore(item);
                    }
                    finally
                    {
                        //确保Shown事件异常时,Modal的结果不受影响
                        Dispatcher.PushFrame(item._dispatcherFrame);
                    }
                    return(item.ModalResult);
                }
            }
            finally
            {
                //确保ComponentDispatcher有进有出
                if (notcanceled)
                {
                    ComponentDispatcher.PopModal();
                }
            }

            return(null);
        }
        public ModalResult ShowModal(PopupItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (this.VerifyIsProcessDialogBox(item))
            {
                throw new Exception($"视图类型 {typeof(ProcessDialogBox)} 不可以模态方式进行显示。");
            }

            //以模态显示窗口时,若发现最上层是模态窗口
            //当顶级窗口为MessageDialogBox时不予显示当前窗口
            //当顶级窗口不是MessageDialogBox时,若当前为MessageDialogBox,则显示
            if (this.VerifyTopItemModal())
            {
                if (this.VerifyTopItemIsMessageDialogBox() ||
                    !this.VerifyIsMessageDialogBox(item))
                {
                    PopupItemContainer container = this.PopupContainerFromIndex(this._popupStack.Items.Count - 1);
                    if (container != null)
                    {
                        container.Flicker();
                    }

                    return(null);
                }
            }

            item.VerifyCanShow(this);

            item._showingAsModal = true;
            item._modalResult    = null;
            CancelEventArgs ce = null;

            try
            {
                item.InternalShowing(out ce);
            }
            catch (Exception)
            {
                item._showingAsModal = false;
                throw;
            }

            try
            {
                if (!ce.Cancel)
                {
                    ComponentDispatcher.PushModal();
                    item._dispatcherFrame = new DispatcherFrame();
                    item.ParentHostStack  = this;
                    item._isClosed        = false;
                    this.AddItem(item);
                    item.InternalShown(out EventArgs e);
                    Dispatcher.PushFrame(item._dispatcherFrame);
                    return(item.ModalResult);
                }
            }
            finally
            {
                ComponentDispatcher.PopModal();
                item.InternalDiapose();
            }

            return(null);
        }