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

热门教程

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

时间:2022-06-25 08:42:44 编辑:袖梨 来源:一聚教程网

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

 

重载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();
            }

}

热门栏目