C#UWP-文本框与大量文本挂在焦点上

本文关键字:文本 焦点 C#UWP- | 更新日期: 2023-09-27 18:29:43

我正在构建一个通用的windows 10应用程序,该应用程序允许用户从文件系统中打开纯文本文件,并将其显示在文本框中。通常情况下,这很好,但现在我正在调查用户打开大量文件的可能性。为了进行测试,我使用了一个大约11000行长的文件。FileIO在大约1秒内将文件加载到字符串中,字符串几乎立即显示在文本框中,但随后应用程序挂起6或7秒,之后我才能正常使用文本框。经过调查,我发现每当文本框聚焦或未聚焦时,应用程序都会像这样挂起。所以我的问题是,这是UWP的限制,还是我在这里缺少了什么?如果是API限制,那没关系,我只需要限制输入文件的大小。我问的原因是,文本框这样滞后于UI似乎不太合适。记事本和记事本++都能很好地处理文件。谢谢


主页.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>

主页.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;
    }
}

C#UWP-文本框与大量文本挂在焦点上

我不确定它是否能解决问题,但我想你应该尝试使用数据绑定({binding}{x:Bind})设置TextBlock.Text值,而不是仅仅从代码后面执行TextBox.Text="blahblahblah"