c# UWP自动滚动文本 [英] c# UWP autoscrolling text

查看:28
本文介绍了c# UWP自动滚动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 raspberry Pi (windows IoT) 构建一个应用程序,它接受 UDP 消息并将它们显示在屏幕上.

I'm currently building an application for raspberry Pi (windows IoT) which accepts UDP messages and shows them on the screen.

我需要一种方法让文本自动在屏幕上水平滚动.我不能让用户点击按钮,因为没有连接到 Pi 的输入设备.
到目前为止,我一直在玩弄滚动查看器并手动调整它的 Horizo​​ntalAlignment 值,但无济于事(我对整个 UWP/XAML 内容有点陌生).

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.
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).

谁能给我看一些代码,使文本块中的文本自动从右向左滚动(很像文本在数字显示器上滚动的方式),而不会中断应用程序中运行的任何其他代码(接收udp 消息和滴答计时器)?

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)?

非常感谢.

推荐答案

您可以在 ScrollViewer 内部设置 TextBlock 以便它可以滚动,例如像这样:

You can set the TextBlock inside of a ScrollViewer so can it to be scrolled for example like this:

<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>

并且在后面的代码中使用 DispatcherTimer 来设置一个用于滚动的计时器,在 ScrollViewerLoaded event 中启动这个计时器并在 ScrollViewerUnloaded 事件 中停止这个计时器:

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:

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();
}

注意到您的应用程序适用于 raspberry Pi,刚刚在 RP2,操作系统版本 10.0.14376.0 上测试了此代码,它工作正常.

Noticed your app is for raspberry Pi, just tested this code on RP2, OS version 10.0.14376.0, it works fine.

这篇关于c# UWP自动滚动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆