简体   繁体   English

向StackPanel添加按钮,导航到页面时滞后

[英]Adding buttons to StackPanel, lag when navigating to page

If I'm adding buttons to a StackPanel, when exactly should I run the following code? 如果我要向StackPanel添加按钮,那么什么时候应该运行以下代码? As it stands just now it's in my OnNavigatedTo function. 就目前而言,它位于我的OnNavigatedTo函数中。 I'm having to declare that with the "async" property as I've got some code to fetch records from a SQLite DB. 我必须使用“ async”属性进行声明,因为我有一些代码可以从SQLite数据库中获取记录。

The problem is that when I access this "view record" page, I can see the buttons being drawn one by one (so it takes only a second but there is a definite lag, there's no buttons, then one, then two, then three etc.), the display doesn't appear with the buttons already present, they are added one at a time and it's pretty obvious (this is when using the emulator and an actual device). 问题是,当我访问此“查看记录”页面时,可以看到按钮被一个接一个地绘制(因此只需要一秒钟,但是有一定的滞后性,没有按钮,然后是一个,然后是两个,然后是三个)等等),则该显示不会与已经存在的按钮一起出现,而是一次添加一个按钮,这非常明显(这是在使用仿真器和实际设备时)。 I wondered if I was just mis-using the OnNavigatedTo event and should be running this code earlier? 我想知道是否只是在滥用OnNavigatedTo事件,是否应该更早地运行此代码?

I did try doing most of it in the constructor but for some reason I couldn't access the NavgiationContext object (I think that's what it was), for getting the record id from the query string, and I don't think I can declare the constructor with the aysnc property. 我确实尝试在构造函数中完成大部分操作,但是由于某些原因,我无法访问NavgiationContext对象(我想就是这样),以便从查询字符串中获取记录ID,而且我认为我无法声明具有aysnc属性的构造函数。 I think maybe running my code before the initializeComponent() method is called in the constructor could work? 我认为也许在构造函数中调用initializeComponent()方法之前运行我的代码是否可以工作?

            // Add ButtonStackPanel
            StackPanel ButtonStackPanel = new StackPanel();
            ButtonStackPanel.Name = "ButtonStackPanel";
            MainStackPanel.Children.Insert(2, ButtonStackPanel);


            // Add Buttons
            while (await statement.StepAsync())
            {
              Button button = new Button();

              button.Click += this.disease_Click;
              button.Content = Names[statement.GetTextAt(0)];
              button.FontSize = 28;
              button.Height = 80;

              ButtonStackPanel.Children.Add(button);
            }

Just looking for some general advice in terms of the order of doing things and if it's alright to have the bulk of my code in the OnNavigatedTo event method. 只是根据工作顺序以及在OnNavigatedTo事件方法中保留我的大部分代码是否可以找到一些一般建议。

Thanks 谢谢

Currently you add your StackPanel containing all the buttons before adding the buttons to the StackPanel. 当前,在将按钮添加到StackPanel之前,先添加包含所有按钮的StackPanel。 So you could bring the lag away from seeing each button drawn individually by putting that call after the loop: 因此,您可以通过将调用放在循环后面来避免看到单独绘制的每个按钮带来的滞后:

// Add ButtonStackPanel
StackPanel ButtonStackPanel = new StackPanel();
ButtonStackPanel.Name = "ButtonStackPanel";

// Add Buttons
while (await statement.StepAsync())
{
    Button button = new Button();

    button.Click += this.disease_Click;
    button.Content = Names[statement.GetTextAt(0)];
    button.FontSize = 28;
    button.Height = 80;

    ButtonStackPanel.Children.Add(button);
}

MainStackPanel.Children.Insert(2, ButtonStackPanel);

If this takes too long consider adding a loading animation first then remove it before you add the StackPanel with the buttons. 如果花费的时间太长,请考虑先添加加载动画,然后在使用按钮添加StackPanel之前将其删除。

Loading data in the step before is always tricky, you end up pulling logic apart into multiple classes which can turn out to be a nightmare to maintain. 在之前的步骤中加载数据总是很棘手,最终您将逻辑拆分成多个类,这可能是维护的噩梦。 Another option would be to show a loading animation ie: 另一个选择是显示加载动画,即:

var progressBar = new ProgressBar() { IsIndeterminate = true };

MainStackPanel.Children.Insert(1, progressBar);
var stackPanel = new StackPanel();

// ...

MainStackPanel.Children.Remove(progressBar);
MainStackPanel.Children.Insert(1, stackPanel);

That way your user will now that something is still going on eg is being prepared. 这样,您的用户现在将继续进行某些事情,例如正在准备中。

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

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