我正在創建包含多個用戶控件(頁面Insteda)的簡單WPF測試項目。我使用切換器類來瀏覽不同的用戶控件之間。當我導航到不同的頁面時,我觀察到內存消耗保持每增加一個UserControle Navigationand GC都不會被調用。WPF應用程序與多個用戶控件
1.So am i doing something wrong in following code?
2.Which part of the code consuming more memory?
3.Do i need to invoke GC for disposing my UserControls on each new UserControle creation?
If need how can i invoke GC?
public void On_Navigate_Click()
{
UserControle newusercontrole=new UserControle();
DataSet ds = new DataSet();
ds=con.getSome_Datafrom_SQL();//Gets data from SQL via connection class
dataGrid_test.ItemsSource = ds.Tables[0].DefaultView;
Grid.SetColumn(newusercontrole, 1);//dataGrid_test is inside newusercontrole and following is the code to add "this" usercontrol to the main window.
Grid.SetRow(newusercontrole, 1);
Grid.SetZIndex(newusercontrole, 10);
Container.Children.Add(newusercontrole);
}
您正在創建一個新的usercontrol,並在每次單擊該按鈕時將其放入子集合中。以前的UC如何從兒童收藏中刪除?如果還有對prev的引用。 UC,GC不會收集。 – keft
是的,現在我改變了我的代碼。在添加新的UserControl之前,我將刪除以前的UC,如下所示。 Container.Children.Remove(oldusercontrole); 他們仍然有很多內存泄漏。 – ManjuVijayan
Container.Children.Remove(oldusercontrole); 其工作.. 謝謝.. – ManjuVijayan