[添加子级时的xamarin滚动视图

问题描述 投票:1回答:1

我有一个<ScrollView/>,其中包含<Grid/>(为<images/>),当用户接近滚动视图的底部时,我连接到一个网站并下载了下一组图像(实际上,JSON包含ImageSource的链接) ),从而创建一个“无尽”的图像滚动框。

我的问题是,当我下载下一组图像时,该应用程序会暂时挂起,然后滚动框会跳转以赶上新添加的图像。 我如何防止这种“跳跃”?

    private async void OnScrolled(object sender, ScrolledEventArgs e)
    {
        ScrollView scroller = (ScrollView)sender;
        //threshhold == bottom of scrollveiw + height of one image (aka just before it's visible)
        double threashold = (e.ScrollY + scroller.Height) + preview_size;

        //if we touch the threshhold...
        if (threashold > scroller.ContentSize.Height)
        {
            //one row of images
            int TilePreload = (Tiles.Count + ColCount);

            //if the next row exceeds the total available post count, download and append more posts
            if (TilePreload >= Posts.Count)
            {
                //we have reached the end of our postlist, we must get more!
                var results = await Task.Run(()=>FetchResults<List<CPost>>());
                Posts.AddRange( results);
            }

            //then, add the tiles to UI 
            //AddRow();// <- jumpy

            //calling this as a task results in no tiles added, and eventually an execption
            await Task.Run( () => AddRow() );
        }
    }

//将for循环用作函数,因此可以作为任务运行(如果需要)

    public void AddRow()
    {
        for (int i = 0; i < RowCount; i++)
        {
            //wrapper for assigning Image to Grid
            //aka ImageSourec = some URL
            AddTile(i);
        }
    }

注意:FetchResults<T>();或多或少是[]的包装器>

//fyi using System.Net.Http;
public static string GetResponse(string page, Dictionary<String, String> arguments, bool IsPost = false)
{
    HttpMethod Method = IsPost ? HttpMethod.Post : HttpMethod.Get;

    var request = new HttpRequestMessage(Method, page)
    {
        Content = new FormUrlEncodedContent(arguments)
    };

    HttpResponseMessage httpResponse = Client.SendAsync(request).Result;
    if (httpResponse.IsSuccessStatusCode)
    {
        return httpResponse.Content.ReadAsStringAsync().Result;
    }
    return null;
}

我有一个

c# xamarin android-scrollview
1个回答
0
投票

如果要更新UI,则需要在主UI线程上完成。

© www.soinside.com 2019 - 2024. All rights reserved.