[英]I have this problem with a singleton form name FCD Cannot access a disposed object. Object name: 'FCD'.'
這里的代碼錯誤這是另一種形式,其中包含一個按鈕以午餐 FCD 形式
private void Button1_Click(object sender, EventArgs e)
{
f = FCD.getInstance();
f.Show();
}
class FDC 是
public partial class FCD : Form
{
private static FCD instance=null;
private FCD()
{
InitializeComponent();
load();
}
public static FCD getInstance()
{
if(instance == null)
{
instance = new FCD();
}
return instance;
}
問題是當我執行 fcd 表單時,它第一次工作,當我關閉它時我再試一次,他們給了我這個問題
檢查實例IsDisposed
是否。 嘗試以下操作:
從更改:
public static FCD getInstance()
{
if(instance == null)
{
instance = new FCD();
}
return instance;
}
至:
public static FCD getInstance()
{
if(instance == null || instance.IsDisposed)
{
instance = new FCD();
}
return instance;
}
所以經過一番大討論后,我找到了這個解決方案
private void Button1_Click(object sender, EventArgs e)
{
if(f!=null)
if (f.IsDisposed)
{
FCD.setInstanceNull();
}
f = FCD.getInstance();
f.Show();
}
如果表單關閉,我添加一個設置實例 = null 的方法
public static void setInstanceNull()
{
instance = null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.