简体   繁体   English

从基本表单继承时导航缓慢

[英]Slow navigation when inheriting from a base form

I am using a base form that has a menu on it so that I don't have to re-do the code for the whole application. 我使用的是带有菜单的基本表单,因此不必为整个应用程序重新编写代码。

Essentially this form is called StudentBase.cs 本质上,此表单称为StudentBase.cs

Then I have another form called StudentProfile that inherits from StudentBase 然后我有另一种叫做StudentProfile的形式,它继承自StudentBase

    public partial class StudentProfile : StudentBase
    {
       public string selectedPage;
    }

This then inherits the menu that is in StudentBase and I don't have to re-do the menu. 然后,它继承了StudentBase中的菜单,而我不必重新执行菜单。

On the menu, there are buttons for the individual forms. 在菜单上,有用于各个表单的按钮。

So let's say I press on Student Profile I use this to navigate: 假设我按了“学生资料”,然后使用它进行导航:

    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();
          }      
    }

Doing this produces a very laggy result, as well as it looks very glitchy 这样做会产生非常滞后的结果,并且看起来也很毛刺

I override selectedPage in the child forms so in the case of StudentProfile I use: 我在子表单中覆盖了selectedPage,因此在使用StudentProfile的情况下,我使用:

   private void StudentProfile_Load(object sender, EventArgs e)
    {
        selectedPage = "Profile";
    }

I have tested this on my friend's code and his navigation works without lag or glitch. 我已经在朋友的代码上对此进行了测试,并且他的导航工作没有滞后或毛刺。 He didn't do the inheritance on the form 他没有对表格进行继承

The problem with your inheritance solution is that when you create an instance of StudentProfile you also create an instance of the StudentBase form. 继承解决方案的问题是,当您创建StudentProfile的实例时,还会创建StudentBase表单的实例。 You show this new instance and hide the old one. 您显示此新实例并隐藏旧实例。 You now have two instances of StudentBase (one visible and one hidden). 现在,您有两个StudentBase实例(一个可见和一个隐藏)。 As you open more forms from your menu, you get more instances of StudentBase in memory. 当您从菜单中打开更多表单时,您将在内存中获得更多StudentBase实例。 Even though they are hidden they still consume resource. 即使它们被隐藏,它们仍然消耗资源。 This would explain the result you see. 这将解释您看到的结果。

I suggest you do as your friend, which is by the way the typical way child forms are handled from a main menu. 我建议您作为朋友来做,这是从主菜单处理子窗体的典型方式。

So, I couldn't properly figure out how to use the UserControls. 因此,我无法正确找出如何使用UserControls。 I put it on my to-do list so I can try that at the end of the project if I still have time left. 我把它放在待办事项清单上,这样如果我还有时间的话可以在项目结束时尝试一下。

But, I figured out why it was taking so long to move from one navigation to the other. 但是,我想出了为什么从一个导航转到另一个导航要花这么长时间的原因。

I was selecting the student details in the base form using 我正在使用的基本表格中选择学生详细信息

Student student = new Student();
Student studentDetailsFound = student.GetStudent(2);

I never stopped it from selecting from the database every time it navigates to a new form, thus, the two-second delay every time. 每当它导航到一种新形式时,我都不会阻止它从数据库中进行选择,因此每次都会延迟两秒钟。 So there were two options to fix this: Static variables or caching . 因此,有两种方法可以解决此问题: 静态变量或缓存

I used the latter and now it switches to the pages rather fast. 我使用了后者,现在它可以很快地切换到页面。

By adding a transition on the form, it made it a lot smoother on the eyes. 通过在表单上添加过渡效果 ,可以使外观更加平滑。

Also note : if you are getting the data like I am you should first wait for the designing of the form to finish. 另请注意 :如果您像我一样获取数据,则应首先等待表单的设计完成。 So put the GetStudent part into this if: 因此,如果满足以下条件,则将GetStudent部分放入其中:

if (this.Site == null || !this.Site.DesignMode)
{
    studentDetailsFound = GetStudent();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM