C#: 文字滚动输出

image-3298

MainWindow.xaml.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Threading;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string textValue;

        private void run()
        {
                char[] textValueArr = textValue.ToCharArray();
                var i = 0;
                while (i < textValueArr.Length)
                {

                    Thread.Sleep(300);
                    this.result.Dispatcher.Invoke(() =>
                    {
                        var resultText = result.Text + "" + textValueArr[i];
                        result.Text = resultText;
                        result.ScrollToEnd();
                    });

                    i++;
                }
            this.Dispatcher.Invoke(() =>
            {
                btn.IsEnabled = true;
                textInput.IsReadOnly = false;
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
            btn.IsEnabled = false;
            textInput.IsReadOnly = true;
            textValue = textInput.Text;
            Thread thread = new(run);
            thread.Start();
        }
    }
}

MainWindow.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Window x:Class="WpfApp1.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:local="clr-namespace:WpfApp1"
       mc:Ignorable="d"
       Title="文字滚动输出" Height="632" Width="452">
    <Grid Height="622" VerticalAlignment="Top" HorizontalAlignment="Left" Width="900">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="11*"/>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition Width="158*"/>
            <ColumnDefinition Width="200*"/>
            <ColumnDefinition Width="364*"/>
            <ColumnDefinition Width="52*"/>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="textInput" Grid.Column="3" HorizontalAlignment="Center" Margin="0,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="358" Height="182" Grid.ColumnSpan="2" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" FontSize="14" Text="你好,这是一段测试文本。--来自搬砖小码哥." VerticalScrollBarVisibility="Auto"/>
        <Label Content="Label"/>
        <Button x:Name="btn" Grid.Column="3" Content="开始打印" HorizontalAlignment="Center" Margin="0,214,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="110" Width="358" FontSize="22" Background="#FF9D00FB" Foreground="White" FontWeight="Bold" BorderBrush="White" Click="Button_Click"/>
        <TextBox x:Name="result" Grid.Column="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="0,343,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="358" Height="202" FontSize="14" IsReadOnly="True" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" VerticalScrollBarVisibility="Auto"/>

    </Grid>
</Window>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据