简体   繁体   English

如何加快包含大量数据的WPF控件的渲染?

[英]How do I speed up a rendering of a WPF control with a lot of data?

I have a user control that often has 2000 or so component controls (TextBoxes, TextBlocks, ComboBoxes, etc.), and my application doesn't run as fast as I'd like. 我有一个用户控件,该控件通常具有2000个左右的组件控件(TextBoxes,TextBlocks,ComboBoxes等),而我的应用程序运行得并不理想。

While 2000 controls may seem like a lot at first, it is really only representative of a WPF data grid that has 10 columns and 200 rows - a common scenario in many applications. 虽然2000控件乍看起来可能很多,但实际上仅代表具有10列和200行的WPF数据网格-在许多应用程序中很常见。

Here's an extremely simple sample that I believe mimics such a data grid, and I'm seeing a 200ms or so render time: 我相信这是一个非常简单的示例,它模仿了这种数据网格,并且看到了200ms左右的渲染时间:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Stopwatch stopwatch = Stopwatch.StartNew();

        ScrollViewer scrollViewer = new ScrollViewer();
        StackPanel stackPanel = new StackPanel();

        for (int rowIndex = 0; rowIndex < 200; rowIndex++)
            for (int columnIndex = 0; columnIndex < 10; columnIndex++)
                stackPanel.Children.Add(new TextBlock { Text = Guid.NewGuid().ToString() });

        scrollViewer.Content = stackPanel;
        this.Content = scrollViewer;

        UpdateLayout(); // Force a UI layout so we can get an accurate stopwatch reading
        MessageBox.Show(stopwatch.Elapsed.ToString(), "UI Layout Time");
    }

This is as simple as I can think to make this example, but 200 ms is still enough for a user to notice and potentially think the software is slow. 这就像我想出的示例一样简单,但是200毫秒仍然足以让用户注意到并可能认为软件运行缓慢。

If this were a real control with a data grid, the controls would be very likely be more complex than TextBlocks and potentially have further time sucks like IValueConverters and Styles / DataTemplates. 如果这是一个真正的带有数据网格的控件,则该控件很可能比TextBlocks更复杂,并且可能还会占用更多时间,例如IValueConverters和Styles / DataTemplates。

Is there anything I can do to speed up the load time of controls that have a lot of data? 我可以采取什么措施来加快包含大量数据的控件的加载时间?

Check the concept: UI Virtualization . 检查概念: UI虚拟化 It should help if you don't have too much controls on viewable area. 如果您对可见区域没有太多控制,它会有所帮助。

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

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