6

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.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
PippinSmith
  • 79
  • 1
  • 5
  • 1
    Could you include some code that you have already tried? Also, just having a Google of "horizontal scrolling text xaml" will give you tons of code and tutorials... See http://stackoverflow.com/help/how-to-ask for more info on how to ask a good question – Geoff James Jul 18 '16 at 09:53
  • 1
    This workaround should be applicable on UWP as well: http://mikaelkoskinen.net/post/winrt-xaml-automatically-scrolling-listview-to-bottom-and-detecting-when-listview-is-scrolled – Kinani Jul 18 '16 at 10:15

2 Answers2

11

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.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • i am just looking for this, can i set it to scroll first vertically then horizontally then again first vertically and horizontally or both simultaneously – Shubham Sahu Aug 15 '17 at 05:18
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.

Community
  • 1
  • 1
Igor Damiani
  • 1,897
  • 9
  • 12