0

I want to build a book app, a RichTextblock with many RichTextblockOverflows, I create the overflows in code-behind, and add them to the stackpanel in a scrollviewer. I tested my app and found that it finished creating the overflows quickly, but after that it will block for a while(which is unbearable) to show the overflows in UI. I registered the LayoutUpdated event of the stackpanel and found that it would be triggered a long time after overflows were created.

How can I improve the performance? Thanks

                while (true)
                {
                    // for existing columns just set their width
                    var overflow = CreateOverflow(columnWidth, margin, lastOverflow);
                    lastOverflow = overflow;

                    overflow.Measure(availableSize);
                    _layoutPanel.Children.Add(overflow);

                    if (!overflow.HasOverflowContent)
                    {
                        overflow.UpdateLayout();
                    }

                    if (!overflow.HasOverflowContent)
                    {
                        _layoutPanel.LayoutUpdated += _layoutPanel_LayoutUpdated;
                        break;
                    }
                }
Infi Xu
  • 45
  • 4
  • Kind of hard to tell without some code example, I'd hedge a guess at including a background worker for creating the overflows – Sayse Jul 23 '13 at 06:42
  • I've tried that, but it sill blocks the UI until all the overflows added into the stackpanel and the layoutupdated of the stackpanel finished – Infi Xu Jul 23 '13 at 06:53
  • your while loop will be stopping the ui from updating – Sayse Jul 23 '13 at 06:55
  • So how can I do that without a loop, thanks! – Infi Xu Jul 23 '13 at 06:59
  • I'm not sure right now sorry, maybe use a timer and move this code to its interval? – Sayse Jul 23 '13 at 07:13
  • I've tried to run this code in another thread, but the code _layoutPanel.Children.Add(overflow) should be run in UI thread – Infi Xu Jul 23 '13 at 07:33
  • You can marshal that call back to the UI thread manually using CoreDispatcher.RunAsync. You can just do it for this line alone. – Nate Diamond Jul 27 '13 at 22:56

0 Answers0