简体   繁体   English

ScrollToAsync在Xamarin.Forms中不起作用

[英]ScrollToAsync is not working in Xamarin.Forms

I have a simple screen example trying to implement the ScrollToAsync function. 我有一个简单的屏幕示例,尝试实现ScrollToAsync函数。 I can't get the function to scroll at all. 我根本无法滚动该功能。

My XAML: 我的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: C#是:

    [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. 我已经尝试过将ScrollToAsync调用包装在一个异步方法中,因此可以在它之前使用“ await”,但这是行不通的。

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

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. LatestElment:要滚动的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM