繁体   English   中英

ScrollToAsync在Xamarin.Forms中不起作用

[英]ScrollToAsync is not working in Xamarin.Forms

我有一个简单的屏幕示例,尝试实现ScrollToAsync函数。 我根本无法滚动该功能。

我的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>

C#是:

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

我究竟做错了什么? 这一定是我需要做的简单事情。 我已经尝试过将ScrollToAsync调用包装在一个异步方法中,因此可以在它之前使用“ await”,但这是行不通的。

那是因为滚动尚未加载,最好尝试将此方法添加到代码中

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

添加“ 延迟”将对您有所帮助,我将其设置为计时器,对我来说效果很好。

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

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

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

LatestElment:要滚动的元素。

暂无
暂无

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

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