【问题标题】:Xamarin forms refresh carouselview after dowloading data下载数据后 Xamarin 表单刷新轮播视图
【发布时间】:2020-11-12 14:45:43
【问题描述】:

我正在学习 xamarin,

每次下载数据时,我都会尝试刷新 carouselView。

数据已正确下载,但问题是当我下载新数据时,我想在我的 carrouselView 上切换以查看上次数据下载后的新数据。

旋转木马“移动”并将我定位在我之前的位置(索引),即使我想在每次数据下载时位于位置(索引)0 上

如何正确刷新我的轮播视图?

这是我的代码cs:

try
        {

        // trying to refresh by renitializing my carouselView
        Device.BeginInvokeOnMainThread(() =>
        {
            NewsList.Position = 0;
            NewsList.ItemsSource = null;
          
           
        });

            
        // Perform the Web request and get the response
        HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;

        // Data Downloaded
        string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
        JObject joResponse = JObject.Parse(json);
        JArray MyJsonResult = (JArray)joResponse["value"];



          List<NewsModel> newsObj = new List<NewsModel>();
             
                foreach (var j in MyJsonResult)
            {

                    joResponse2 = JObject.Parse(j.ToString());

                    if (joResponse2["image"]!= null)
                    {
                        

                        var news = new NewsModel();
                        news.NewsName = GetFewWordsSentence(joResponse2["name"].ToString()) ;
                        news.NewsDate = joResponse2["datePublished"].ToString();
                        news.NewsImageUrl = joResponse2["image"]["contentUrl"].ToString(); 
                        newsObj.Add(news);

                     // feed my carouselView in the loop for performance
                       // I have already try, to put this line out of the loop but it is not refresh correctly also
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        NewsList.ItemsSource = newsObj;

                   });


                }
               
                }

                    
    }


    catch (Exception e)
    {
        Console.Error.WriteLine(@"ERROR {0}", e.Message);

    }

提前致谢

【问题讨论】:

    标签: c# .net xamarin.forms


    【解决方案1】:

    旋转木马“移动”并将我定位在我之前的位置(索引),即使我想在每次数据下载时位于位置(索引)0 上

    根据您的描述,您下载数据并显示 CarouselView,然后在任意位置将 item 交换为 CarouselView。你想在再次下载数据时显示新数据并转到位置0,对吗?

    如果是,我建议你可以先用 observablecollection 替换 List,它实现了 INotifyPropertychanged 接口来通知数据更新。

      public ObservableCollection<NewsModel> newsObj { get; set; }
    

    然后再次下载数据时清除数据,

     newsObj.Clear();
    

    使用 ScrollTo 将指定索引处的项目滚动到视图中。

     Device.BeginInvokeOnMainThread(()=>
            {
                newsList.ScrollTo(0);
            }
            ) ;
    

    【讨论】:

    • 嗨,它不工作。我有同样的问题。 carouselView 很好地滚动到索引 0,但是一旦我触摸它,它就会移动到上一个位置
    • @hugo 这是我的示例,你可以看看:github.com/CherryBu/carouselview,我没有你的问题。
    • 我希望研究你的代码,Cherry。链接不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多