我们怎样才能为窗口中创建一个Singleton实例? [英] How can we create a Singleton Instance for a Window?
本文介绍了我们怎样才能为窗口中创建一个Singleton实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经寻找建立在WPF窗口的Singleton对象。
<预类=郎-CS prettyprint-覆盖>
公共静态测试DefInstance
{
得到
{
如果(formDefInstance == NULL)// formDefInstance.IsDisposed
{
initializingDefInstance = TRUE;
formDefInstance =新CAS18();
initializingDefInstance = FALSE;
}
返回formDefInstance;
}
集合{formDefInstance =价值; }
}
但 forDefInstance.IsDisposed
不工作,并抛出一个错误。
关于本?
解决方案<任何想法/ DIV>
我不知道这是否是你想要做什么,但它为我的作品:
私有静态mywindow的_defInstance;
公共静态mywindow的DefInstance
{
得到
{
如果(空== _defInstance)
{
_defInstance =新mywindow的();
}
返回_defInstance;
}
}
在mywindow的代码:
保护覆盖无效OnClosing(System.ComponentModel.CancelEventArgs E)
{
this.Visibility = Visibility.Hidden;
e.Cancel = TRUE;
}
要使用它:
DefInstance.Show();
那么,只有一个窗口显示和您使用窗口的一个实例。
I have searched for creating a Singleton object for a window in WPF.
public static Test DefInstance
{
get
{
if (formDefInstance == null) // formDefInstance.IsDisposed
{
initializingDefInstance = true;
formDefInstance = new cas18();
initializingDefInstance = false;
}
return formDefInstance;
}
set { formDefInstance = value; }
}
But the forDefInstance.IsDisposed
is not working and throwing an error.
Any Idea regarding this?
解决方案
I don't know if it's what you want to do but it works for me :
private static MyWindow _defInstance;
public static MyWindow DefInstance
{
get
{
if (null == _defInstance)
{
_defInstance = new MyWindow();
}
return _defInstance;
}
}
In MyWindow code :
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
this.Visibility = Visibility.Hidden;
e.Cancel = true;
}
To use it :
DefInstance.Show();
Then, only one window is display and you use one instance of your window.
这篇关于我们怎样才能为窗口中创建一个Singleton实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文