James Carter
James Carter

Reputation: 83

ScrollToAsync is not working in Xamarin.Forms

I have a simple screen example trying to implement the ScrollToAsync function. I can't get the function to scroll at all.

My XAML:

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyApp.ItemList">
<ContentPage.Content>
    <ScrollView x:Name="myScroll">
    <StackLayout>

        <Label Text="Welcome to Xamarin.Forms!" />
            <Label Text="Welcome to Xamarin.Forms!" />
            <Label Text="Welcome to Xamarin.Forms!" />
            <Label Text="Welcome to Xamarin.Forms!" />
              // Many labels to fill space here
            <Label Text="Welcome to Xamarin.Forms!" />
        </StackLayout>
    </ScrollView>
</ContentPage.Content>
</ContentPage>

And the C# is:

    [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemList : ContentPage
{
    public ItemList ()
    {
        InitializeComponent ();
        myScroll.ScrollToAsync(0, 100, false);
    }
}

What am I doing wrong? It must be something simple I need to do. I already tried wrapping the ScrollToAsync call in an async method, so I can use "await" before it, but this did not work.

Upvotes: 0

Views: 2605

Answers (2)

user10839908
user10839908

Reputation:

Adding Delay would helped You , I set it in a timer and worked fine for me .

public MainPage()
    {
        InitializeComponent();
        StarAnimate();
    }

   private void StarAnimate()
    {
        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {

            myScroll.ScrollToAsync(LatestElment, ScrollToPosition.Center, true);
            return false; 
        });
    }

LatestElment:The element to scroll.

Upvotes: 1

Javier Trancoso Lara
Javier Trancoso Lara

Reputation: 81

That is because the scroll is not loaded yet, better try to add this method to your code

protected override void OnAppearing()
    {
        base.OnAppearing();
        scLista.ScrollToAsync(0,100,false);
    }

Upvotes: 2

Related Questions