[英]C# UWP - Textbox hangs on focus with large amount of text
我正在构建一个通用的Windows 10应用程序,该应用程序使用户可以从文件系统中打开纯文本文件并将其显示在文本框中。 通常,这可以正常工作,但是现在我正在研究用户打开海量文件的可能性。 为了进行测试,我使用的文件长约11,000行。 FileIO
在大约1秒钟的时间内将文件加载到字符串中,并且该字符串几乎立即显示在文本框中,但是应用程序挂起6或7秒钟,只有在此之后我才能正常使用该文本框。 经过调查,我发现每当文本框被聚焦或不聚焦时,应用程序都会像这样挂起。 所以我的问题是,这是UWP的限制吗?还是我在这里缺少什么? 如果这是API限制,那很好,我只需要限制输入文件的大小即可。 我要问的原因是,这样的文本框滞后于UI似乎不太正确。 记事本和notepad ++都可以很好地处理文件。 谢谢!
MainPage.xaml中
<Page x:Class="App7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibiliy/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Click="Button_Click" VerticalAlignment="Top">Browse</Button>
<TextBox x:Name="TextBox1" Margin="0,50,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AcceptsReturn="True" />
</Grid>
</Page>
MainPage.xaml.cs中
这是默认模板的唯一补充
private async void Button_Click(object sender, RoutedEventArgs e) {
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(".txt");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null) {
// Not the issue
string text = await FileIO.ReadTextAsync(file);
// This causes the textbox to be focused, or at least has the same effect
this.TextBox1.Text = text;
}
}
我不确定它是否可以解决问题,但我想您应该尝试使用数据绑定( {binding}
和{x:Bind}
)设置TextBlock.Text值,而不是仅仅从TextBox.Text="blahblahblah"
代码隐藏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.