[英]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.