简体   繁体   English

c#UWP自动滚动文本

[英]c# UWP autoscrolling text

I'm currently building an application for raspberry Pi (windows IoT) which accepts UDP messages and shows them on the screen. 我正在构建一个覆盆子Pi(Windows IoT)的应用程序,它接受UDP消息并在屏幕上显示它们。

I need a way to make the text horizontally scroll over the screen automatically. 我需要一种方法让文本自动水平滚动屏幕。 I can't let the user click a button because there are no input devices connected to the Pi. 我无法让用户点击按钮,因为没有连接到Pi的输入设备。
So far I've been toying around with a scrollviewer and adjusting it's HorizontalAlignment value manually, but with no avail (I'm kinda new to the whole UWP/XAML stuff). 到目前为止,我一直在玩一个滚动查看器并手动调整它的Horizo​​ntalAlignment值,但没有用(我对整个UWP / XAML的东西都是新手)。

Can anyone show me some code that would make the text in the textblock automatically scroll from right to left (much in the way text scrolls on digital displays) that doesn't interrupt any of the other code running in the app (receiving udp messages and ticking the timer)? 任何人都可以向我展示一些代码,这些代码可以使文本块中的文本自动从右向左滚动(文本在数字显示上滚动的方式),不会中断应用程序中运行的任何其他代码(接收udp消息和勾选计时器)?

Many thanks in advance. 提前谢谢了。

You can set the TextBlock inside of a ScrollViewer so can it to be scrolled for example like this: 您可以在ScrollViewer设置TextBlock ,因此可以滚动它,例如:

<ScrollViewer x:Name="scrollviewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden"
              VerticalScrollMode="Disabled" HorizontalScrollMode="Enabled" Grid.Row="1" Loaded="scrollViewer_Loaded"
              Unloaded="scrollviewer_Unloaded">
    <TextBlock Text="Start:111fdafdilgklnkghiogj2222213135aaaadjiosfuiafkhafuia464676541134564132145546afafkjarpikfsanjahfnvfnvjkhghga:End" TextWrapping="NoWrap"
             FontSize="40" />
</ScrollViewer>

And in the code behind use a DispatcherTimer to set a timer for scrolling, in the Loaded event of the ScrollViewer start this timer and in the Unloaded event of the ScrollViewer stop this timer: 并且在后面的代码中使用DispatcherTimer设置滚动计时器,在ScrollViewerLoaded event中启动此计时器并在ScrollViewerUnloaded event中停止此计时器:

private void scrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    timer.Tick += (ss, ee) =>
    {
        if (timer.Interval.Ticks == 300)
        {
            //each time set the offset to scrollviewer.HorizontalOffset + 5
            scrollviewer.ScrollToHorizontalOffset(scrollviewer.HorizontalOffset + 5);
            //if the scrollviewer scrolls to the end, scroll it back to the start.
            if (scrollviewer.HorizontalOffset == scrollviewer.ScrollableWidth)
                scrollviewer.ScrollToHorizontalOffset(0);
        }
    };
    timer.Interval = new TimeSpan(300);
    timer.Start();
}

private void scrollviewer_Unloaded(object sender, RoutedEventArgs e)
{
    timer.Stop();
}

Noticed your app is for raspberry Pi, just tested this code on RP2, OS version 10.0.14376.0, it works fine. 注意到您的应用程序是针对raspberry Pi,只是在RP2,OS版本10.0.14376.0上测试了此代码,它工作正常。

See this post: Multiline Textbox with automatic vertical scroll 看这篇文章: 带有自动垂直滚动的多行文本框

Replace the TextBlock with a readonly TextBox, so you can programmatically scroll to the end at each update of his content. 使用只读TextBox替换TextBlock,以便您可以在每次更新其内容时以编程方式滚动到末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM