コード例 #1
0
        void window_Closing(object sender, CancelEventArgs e)
        {
            // 如果是 NavOut 导致的关闭,则不作任何处理,直接关闭,不会触发 WannaClose 事件
            // 否则认为是用户点击右上角的关闭按钮,发出 WannaClose 通知交由外部处理

            bool userClickCloseButton = !WindowAddtionInfo.GetCloseBecauseNavOut(window);

            if (userClickCloseButton)
            {
                if (WannaClose != null)
                {
                    WindowAddtionInfo.SetAcceptClose(window, false);
                    WindowAddtionInfo.SetInClosingCallStack(window, true);
                    try
                    {
                        WannaClose(this, EventArgs.Empty);
                    }
                    finally
                    {
                        WindowAddtionInfo.SetInClosingCallStack(window, false);
                    }
                }

                e.Cancel = !WindowAddtionInfo.GetAcceptClose(window);
            }
        }
コード例 #2
0
        public void NavOut()
        {
            // 如果根本就没有任何窗体,则忽略本次调用

            if (window == null)
            {
                return;
            }

            // 子类将重写 beforeNavOut() 以完成窗体的初始化等工作

            beforeNavOut();

            // 执行关闭窗口的操作

            if (WindowAddtionInfo.GetInClosingCallStack(window))
            {
                // 当前调用是位于 Closing 调用栈里
                // 我们只需把相关字段设为 true 就可以完成关闭
                // 不能去调用 Close() 方法,否则会抛出异常

                WindowAddtionInfo.SetAcceptClose(window, true);
            }
            else
            {
                // 直接调用 Close() 就可以关闭窗口了
                // 为了避免事件重复触发,这里需要在 Close() 前做个标记

                WindowAddtionInfo.SetCloseBecauseNavOut(window, true);
                window.Close();
            }
        }
コード例 #3
0
        static public bool GetInClosingCallStack(Window window)
        {
            WindowAddtionInfo info = null;

            info = window.Tag as WindowAddtionInfo;

            return(info != null ? info.InClosingCallStack : false);
        }
コード例 #4
0
        static public bool GetAcceptClose(Window window)
        {
            WindowAddtionInfo info = null;

            info = window.Tag as WindowAddtionInfo;

            return(info != null ? info.AcceptClose : false);
        }
コード例 #5
0
        static public bool GetCloseBecauseNavOut(Window window)
        {
            WindowAddtionInfo info = null;

            info = window.Tag as WindowAddtionInfo;

            return(info != null ? info.CloseBecauseNavOut : false);
        }
コード例 #6
0
        static private WindowAddtionInfo retriveOrCreateInfo(Window window)
        {
            if (window.Tag != null && window.Tag as WindowAddtionInfo == null)
            {
                throw new InvalidOperationException();
            }

            WindowAddtionInfo info = window.Tag as WindowAddtionInfo;

            if (info == null)
            {
                window.Tag = info = new WindowAddtionInfo();
            }

            return(info);
        }