Reputation: 446
I use Xamarin.Forms and I want create movable label on my application on realtime (on timer). I create this logic:
XAML code:
<ScrollView Grid.Row="0" VerticalOptions="End"
Orientation="Horizontal"
HorizontalScrollBarVisibility="Never"
Margin="0, 0, 0, 40"
x:Name="TickerTitleSound"
IsEnabled="False"
>
<Label
Text="{Binding SpecialText}" />
</ScrollView>
And C# code behind:
// Direction - bool class member
TickerTitleSound.Scrolled += (sender, e) =>
{
double scrollingSpace = TickerTitleSound.ContentSize.Width - TickerTitleSound.Width;
if(TickerTitleSound.ScrollX >= scrollingSpace)
{
Direction = false;
}
else if(TickerTitleSound.ScrollX <= 0)
{
Direction = true;
}
};
Timer timerSound = new Timer() { Interval = 100 };
timerSound.Elapsed += async (o, e) =>
{
await Task.Run(() =>
{
Device.BeginInvokeOnMainThread(() =>
{
if (Direction)
{
TickerTitleSound.ScrollToAsync(TickerTitleSound.ScaleX + 10, 0, false);
}
else
{
TickerTitleSound.ScrollToAsync(TickerTitleSound.ScaleX - 10, 0, false);
}
});
});
};
timerSound.Start();
But this code not working. I want get this result:
How this make? I have
Upvotes: 0
Views: 89
Reputation: 1934
You can set the TranslationX of label, here is my sample code below:
public partial class TestPage1 : ContentPage
{
private bool Execute { get; set; }
public TestPage ()
{
InitializeComponent ();
Label1.Text = "This is to simulate a really long sentence for testing purposes";
Label1.HorizontalOptions = LayoutOptions.Start;
Label1.VerticalTextAlignment = TextAlignment.Center;
Label1.LineBreakMode = LineBreakMode.NoWrap;
}
protected override void OnAppearing()
{
base.OnAppearing();
Execute = true;
Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
{
Label1.TranslationX -= 5f;
if (Math.Abs(Label1.TranslationX) > Width)
{
Label1.TranslationX = Label1.Width;
}
return Execute;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Execute = false;
}
}
Upvotes: 1