我正在爲一家公司開發「緩存圖像生成器」。
這種形式的目標是預先計算所有可能的按鈕場景。
它創建了幾個自定義按鈕,拍攝快照,清除所有內容並重做,直到全部嘗試完畢。無法銷燬我的按鈕標籤
我的問題是,我不能銷燬我的自定義按鈕內的標籤。
我的自定義按鈕工作正常,我可以生成第一個圖像。
因爲我使用索引作爲標籤名稱的最後一部分,我相信,第二回合將由於現有的具有相同名稱的項目而失敗。
這是如何我試圖毀滅一切:
foreach (my_button b in this.wrapper.Controls.OfType<my_button>())
{
b.resume_layout();
b.show();
}
this.PerformLayout();
bmp = new Bitmap(this.wrapper.Width, this.wrapper.Height);
this.wrapper.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
a.result = bmp;
cc = this.wrapper.Controls;
this.wrapper.Controls.Clear();
foreach (Control C in cc)
C.Dispose();
cc = null;
GC.Collect();
,這是自定義處置my_button的處理程序:
new public void Dispose()
{
this.Dispose(true);
}
protected override void Dispose(bool disposing)
{
this.currency_label.Dispose();
this.name_label.Dispose();
this.price_label.Dispose();
this.currency_label = this.name_label = this.price_label = null;
this.BackgroundImage = null;
this.Controls.Clear();
base.Dispose(disposing);
}
我認爲,這是一個不破壞麻煩,因爲我隨機獲取:
Illegal cross-thread operation: Control 'control name' accessed from a thread other than the thread it was created on.
標籤Dispose()調用。
在此先感謝您的幫助。
- 編輯 -
我解決了。
問題是我mooremachine框架錯誤的呼叫設置一切可見=假
請刪除這個問題,因爲它不會是有用的。
'GC.Collect'從來就不是一個好主意。我想你想使用'cc.Controls.Remove(c)'(現在不確定正確的語法) – Sayse
Button b = new Button(); ContentControl c = b作爲ContentControl; c.Content = null; –