[WPF疑难]Hide me! not close

原文 [WPF疑难]Hide me! not close

[WPF疑难]Hide me! not close
                              周银辉

有朋友遇到这样的一个问题,在WPF中,当Closing一个窗体时,将e.Cancel=true,然后再调用Hide()方法,以便隐藏窗口而不是关
闭,但报异常了:“当Window
Closing时不能设置Visibility,或调用Show(),Close(),Hide()方法”。OK,本随笔将帮你解决该问题。

问题的关键在于不能再Closing方法中调用Close等,那么只要我们知道用户有意图关闭窗体时,仅仅再Closing方法中取消关闭,然后在
Closing紧接着的某个方法中调用Hide就OK了。为了体现这个“紧接着的某个方法”,让我联想到方法排队,比如多个线程中的方法使用同一个对象
时,这些方法将被排队,否则异常。那么就用Invoke来帮我们实现这个排队就OK了。

假设我们的Window类型的win2时一个需要隐藏的窗口,企图关闭该窗体时其会被隐藏,点击主窗口上的btnShowWin2按钮时窗体会再次被显示。
我们实现一个Delegate,其代理的方法将异常窗体:

[WPF疑难]Hide me! not closedelegate void WillHide();
[WPF疑难]Hide me! not close//[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not closeprivate WillHide willHide;
[WPF疑难]Hide me! not close//[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not closethis.willHide = new WillHide(this.HideWin2);
[WPF疑难]Hide me! not close//[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not closeprivate void HideWin2()
[WPF疑难]Hide me! not close{
[WPF疑难]Hide me! not close     this.win2.Hide();
[WPF疑难]Hide me! not close}
[WPF疑难]Hide me! not close

当Closing时我们这样:

[WPF疑难]Hide me! not close        void win2_Closing(object sender, CancelEventArgs e)
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            e.Cancel = true;
[WPF疑难]Hide me! not close            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, this.willHide);
[WPF疑难]Hide me! not close        }

Everything is OK!

整体的代码:

[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not closeusing System;
[WPF疑难]Hide me! not closeusing System.Collections.Generic;
[WPF疑难]Hide me! not closeusing System.Linq;
[WPF疑难]Hide me! not closeusing System.Text;
[WPF疑难]Hide me! not closeusing System.Windows;
[WPF疑难]Hide me! not closeusing System.Windows.Controls;
[WPF疑难]Hide me! not closeusing System.Windows.Data;
[WPF疑难]Hide me! not closeusing System.Windows.Documents;
[WPF疑难]Hide me! not closeusing System.Windows.Input;
[WPF疑难]Hide me! not closeusing System.Windows.Media;
[WPF疑难]Hide me! not closeusing System.Windows.Media.Imaging;
[WPF疑难]Hide me! not closeusing System.Windows.Navigation;
[WPF疑难]Hide me! not closeusing System.Windows.Shapes;
[WPF疑难]Hide me! not closeusing System.ComponentModel;
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not closenamespace ClosingDemo
[WPF疑难]Hide me! not close{
[WPF疑难]Hide me! not close    /// <summary>
[WPF疑难]Hide me! not close    /// Interaction logic for Window1.xaml
[WPF疑难]Hide me! not close    /// </summary>
[WPF疑难]Hide me! not close    public partial class Window1 : Window
[WPF疑难]Hide me! not close    {
[WPF疑难]Hide me! not close        delegate void WillHide();
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        private Window2 win2 = new Window2();
[WPF疑难]Hide me! not close        private WillHide willHide;
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        public Window1()
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            InitializeComponent();
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close            Test();
[WPF疑难]Hide me! not close        }
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        private void HideWin2()
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            this.win2.Hide();
[WPF疑难]Hide me! not close        }
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        private void Test()
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            App.Current.MainWindow = this;
[WPF疑难]Hide me! not close            App.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close            this.willHide = new WillHide(this.HideWin2);
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close            this.win2.Closing += new CancelEventHandler(win2_Closing);
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close            this.btnShowWin2.Click += new RoutedEventHandler(btnShowWin2_Click);
[WPF疑难]Hide me! not close           
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close            this.win2.Show();
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        }
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        void btnShowWin2_Click(object sender, RoutedEventArgs e)
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            this.win2.Show();
[WPF疑难]Hide me! not close        }
[WPF疑难]Hide me! not close
[WPF疑难]Hide me! not close        void win2_Closing(object sender, CancelEventArgs e)
[WPF疑难]Hide me! not close        {
[WPF疑难]Hide me! not close            e.Cancel = true;
[WPF疑难]Hide me! not close            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, this.willHide);
[WPF疑难]Hide me! not close        }
[WPF疑难]Hide me! not close        
[WPF疑难]Hide me! not close       
[WPF疑难]Hide me! not close    }
[WPF疑难]Hide me! not close}
[WPF疑难]Hide me! not close
上一篇:WebSocket聊天室demo


下一篇:Sprint总结和第八九十的读书笔记