是否有可能在单例模式后创建模态对话框?
这个想法是:
public partial class Singleton : Form
{
private static Singleton _instance = null;
private Singleton()
{
// Initialization code
}
public static Singleton Instance
{
get
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
}
private void Singleton_FormClosing(object sender, FormClosingEventArgs e)
{
_instance.Hide();
e.Cancel = true;
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
如果表单是非模态的(因此,如果使用Show()方法),此代码可以正常工作,但如果表单是模态的(因此,如果使用ShowDialog()方法)则不起作用,因为这也会隐藏父表格.
继续我的评论,不要这样做.不要使表单/对话框成为单例.该对话框应该只显示您要显示的数据.数据的缓存应该在其他地方处理1.因此,在创建对话框时,将其传递给您希望它显示的缓存对象.基本上,使用MVC模式.
| 归档时间: |
|
| 查看次数: |
890 次 |
| 最近记录: |