ScrollToAsync 在 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”,但这不起作用。


胡子哥哥
浏览 458回答 2
2回答

一只萌萌小番薯

那是因为滚动尚未加载,最好尝试将此方法添加到您的代码中protected override void OnAppearing()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnAppearing();&nbsp; &nbsp; &nbsp; &nbsp; scLista.ScrollToAsync(0,100,false);&nbsp; &nbsp; }

白猪掌柜的

添加延迟会对您有所帮助,我将其设置在计时器中并且对我来说效果很好。public MainPage()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; StarAnimate();&nbsp; &nbsp; }&nbsp; &nbsp;private void StarAnimate()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Device.StartTimer(TimeSpan.FromSeconds(1), () =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myScroll.ScrollToAsync(LatestElment, ScrollToPosition.Center, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }LatestElment:要滚动的元素。
打开App,查看更多内容
随时随地看视频慕课网APP