[英]Slow navigation when inheriting from a base form
我使用的是帶有菜單的基本表單,因此不必為整個應用程序重新編寫代碼。
本質上,此表單稱為StudentBase.cs
然后我有另一種叫做StudentProfile的形式,它繼承自StudentBase
public partial class StudentProfile : StudentBase
{
public string selectedPage;
}
然后,它繼承了StudentBase中的菜單,而我不必重新執行菜單。
在菜單上,有用於各個表單的按鈕。
假設我按了“學生資料”,然后使用它進行導航:
private void btnProfile_Click(object sender, EventArgs e)
{
//I don't want the page to reload if it is the current page
if (selectedPage != "Profile")
{
StudentProfile profile = new StudentProfile();
profile.Show();
this.Hide();
}
}
這樣做會產生非常滯后的結果,並且看起來也很毛刺
我在子表單中覆蓋了selectedPage,因此在使用StudentProfile的情況下,我使用:
private void StudentProfile_Load(object sender, EventArgs e)
{
selectedPage = "Profile";
}
我已經在朋友的代碼上對此進行了測試,並且他的導航工作沒有滯后或毛刺。 他沒有對表格進行繼承
繼承解決方案的問題是,當您創建StudentProfile
的實例時,還會創建StudentBase
表單的實例。 您顯示此新實例並隱藏舊實例。 現在,您有兩個StudentBase
實例(一個可見和一個隱藏)。 當您從菜單中打開更多表單時,您將在內存中獲得更多StudentBase
實例。 即使它們被隱藏,它們仍然消耗資源。 這將解釋您看到的結果。
我建議您作為朋友來做,這是從主菜單處理子窗體的典型方式。
因此,我無法正確找出如何使用UserControls。 我把它放在待辦事項清單上,這樣如果我還有時間的話可以在項目結束時嘗試一下。
但是,我想出了為什么從一個導航轉到另一個導航要花這么長時間的原因。
我正在使用的基本表格中選擇學生詳細信息
Student student = new Student();
Student studentDetailsFound = student.GetStudent(2);
每當它導航到一種新形式時,我都不會阻止它從數據庫中進行選擇,因此每次都會延遲兩秒鍾。 因此,有兩種方法可以解決此問題: 靜態變量或緩存 。
我使用了后者,現在它可以很快地切換到頁面。
通過在表單上添加過渡效果 ,可以使外觀更加平滑。
另請注意 :如果您像我一樣獲取數據,則應首先等待表單的設計完成。 因此,如果滿足以下條件,則將GetStudent部分放入其中:
if (this.Site == null || !this.Site.DesignMode)
{
studentDetailsFound = GetStudent();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.