猿问

将文本属性设置为大字符串时,WPF 文本框元素会冻结

我目前正在开发一个 UWP 应用程序,我遇到了 TextBox WPF 元素的问题,它在处理较大的文本时似乎性能很差。将剪贴板中的大文本粘贴到文本框中效果很好,但是当我尝试以编程方式设置文本时,它会冻结整个应用程序:

textbox.Text = sb.ToString(); //Freezes or takes for ever

这就是我在 MainPage.xaml 中定义文本框的方式:

<TextBox x:Name="textbox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Margin="0,0,0,70"/>

有没有人知道这个性能问题的解决方案,或者有没有我可以使用的替代文本字段元素?


暮色呼如
浏览 222回答 1
1回答

慕的地10843

好的...我已经能够获得添加文本所需的时间,最多需要 133 毫秒,这是我发现的最高时间。这是一个轻微的抖动;如果我添加 50K 个字符,则需要大约 1000 毫秒,这很明显,但考虑到字符数,这并不坏。为了执行测试,我首先创建了一个视图: ProgressBar检测 UI 冻结/抖动。 TextBlock以毫秒为单位显示添加文本所需的时间。 Button开始添加文本。 TextBox将文本添加到。主页.xaml<Page x:Class="Question_Answer_UWPApp.MainPage"&nbsp; &nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; &nbsp; mc:Ignorable="d">&nbsp; &nbsp; <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">&nbsp; &nbsp; &nbsp; &nbsp; <ProgressBar Name="progressBarAddingText"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IsIndeterminate="True" />&nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Name="textBlockAddingTextTime" />&nbsp; &nbsp; &nbsp; &nbsp; <Button Content="Add Text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click="ButtonClick" />&nbsp; &nbsp; &nbsp; &nbsp; <TextBox Name="textBoxAddingText"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HorizontalAlignment="Stretch"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VerticalAlignment="Stretch"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ScrollViewer.VerticalScrollBarVisibility="Auto"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextWrapping="Wrap"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Margin="0,0,0,70" />&nbsp; &nbsp; </StackPanel></Page>主页.xaml.csusing System.Diagnostics;using System.Text;using System.Threading.Tasks;using Windows.UI.Xaml.Controls;namespace Question_Answer_UWPApp{&nbsp; &nbsp; public sealed partial class MainPage : Page&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static Stopwatch stopwatch = new Stopwatch();&nbsp; &nbsp; &nbsp; &nbsp; public MainPage()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public async void AddTextAsynchronously()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var text = await Task.Run(() =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var stringBuilder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5000; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.Append("X");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return stringBuilder.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stopwatch.Start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textBoxAddingText.Text = text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stopwatch.Stop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textBlockAddingTextTime.Text = $"{stopwatch.ElapsedMilliseconds.ToString()} milliseconds";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void ButtonClick(object sender, Windows.UI.Xaml.RoutedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddTextAsynchronously();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在这个测试中,我得到了大约 133 毫秒,但是当我放置StringBuilder外部Task并使用Taskjust 来构建StringBuilder(基本上让我说它.Text = stringBuilder.ToString()需要大约 10 毫秒。这意味着这StringBuilder不是瓶颈。我不相信将文本添加到TextBox也是您的瓶颈。通过这个测试,我觉得你应该在其他地方寻找你的瓶颈。这应该可以安全地回答向TextBoxvia 代码添加文本并不耗时的问题。
随时随地看视频慕课网APP
我要回答