ScrollToAsync 在 Xamarin.Forms 中不起作用 [英] ScrollToAsync is not working in Xamarin.Forms

查看:63
本文介绍了ScrollToAsync 在 Xamarin.Forms 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的屏幕示例,试图实现 ScrollToAsync 功能.我根本无法获得滚动功能.

I have a simple screen example trying to implement the ScrollToAsync function. I can't get the function to scroll at all.

我的 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",但这不起作用.

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.

推荐答案

那是因为scroll还没有加载,最好尝试在你的代码中加入这个方法

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

这篇关于ScrollToAsync 在 Xamarin.Forms 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆