一聚教程网:一个值得你收藏的教程网站

最新下载

wpf单例模式只打开一个窗口例子

时间:2016-08-17 00:00:00 编辑:简简单单 来源:转载

WPF  单例窗口Close()后再Show()会报错,Close会调用Dispose,资源会马上被释放,ShowDialog()的情形是一样的。

wpf单例模式只打开一个窗口例子

 

重载OnClosing(CancelEventArgs e)方法,使用Hide()方法。

 代码如下
 private static GHXTestControl _instance;
        private static readonly object ObjLok = new object();
        public static GHXTestControl Instance()
        {
            lock (ObjLok)
            {
                return _instance ?? (_instance = new GHXTestControl());
            }
        }
        /// 重写Close,窗口关闭时设置为隐藏。 
        ///
 
        protected override void OnClosing(CancelEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }
        private GHXTestControl()
        {
            InitializeComponent();
        }

调用:

 代码如下

  private WindowPages.GanHuaxue.GHXTestControl _GHXControl;
        public WindowPages.GanHuaxue.GHXTestControl GHXControl
        {
            get
            {
                if (_GHXControl == null)
                    _GHXControl =   WindowPages.GanHuaxue.GHXTestControl.Instance();
                return _GHXControl;
            }
        }

        private void Click()
        {
            if (CtrButtonType == CometButtonType.干化学)
            {
                /*WindowPages.SystemManagement.Test t = new WindowPages.SystemManagement.Test("myArg1", "myArg2");
                t.BtnEvent += s1; //弹出的页面 点击关闭按钮时
                t.ShowDialog();
                */

                //WindowPages.GanHuaxue.GHXTestControl t = new WindowPages.GanHuaxue.GHXTestControl();
                GHXControl.AddInspect(ThisBtn);
                GHXControl.ShowActivated=true;
                GHXControl.Show();
                GHXControl.Activate();
            }

}

网站留言

热门栏目