Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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

Many thanks in advance.

like image 615
PippinSmith Avatar asked Jul 18 '16 09:07

PippinSmith


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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>

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

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

like image 54
Grace Feng Avatar answered Sep 23 '22 13:09

Grace Feng