ScrollToAsync is not working in Xamarin.Forms

旧城冷巷雨未停 提交于 2020-01-06 07:07:54

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/52231283/scrolltoasync-is-not-working-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!