开发者

How to efficiently stream text to screen in WPF?

开发者 https://www.devze.com 2023-02-03 15:12 出处:网络
I want to stream a bunch of text to display the status/progress of a long running task (such as the output window in Visual Studio).

I want to stream a bunch of text to display the status/progress of a long running task (such as the output window in Visual Studio).

Currently I have something like this XAML:

开发者_运维知识库    <ScrollViewer Canvas.Left="12" Canvas.Top="12" Height="129" Name="scrollViewer1" Width="678">
        <TextBlock Name="text"  TextWrapping="Wrap"></TextBlock>
    </ScrollViewer>

and this code behind:

    private void Update(string content)
    {
        text.Text += content + "\n";
        scrollViewer1.ScrollToBottom();
    }

After a while, it gets really slow.

Is there a recommended way of doing this type of thing? Am I using the right kinds of controls?

Thanks!


At a minimum, you'll want to use a readonly TextBox and use the AppendText() method to append text.

Of course, you're still not immune from performance problems if you have sufficient volumes of text. That being the case, you might need to look into virtualization (both data and UI) solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消