简体   繁体   English

Xamarin.forms 下载数据后刷新轮播视图

[英]Xamarin forms refresh carouselview after dowloading data

I am learning xamarin,我在学习xamarin,

I am trying to refresh my carouselView every time I download data.每次下载数据时,我都试图刷新我的 carouselView。

The data are downloaded correctly, but the problem is when I download new data and I want to swap on my carrouselView to see my new data after a previous data download.数据已正确下载,但问题是当我下载新数据时,我想在我的 carrouselView 上交换以查看上一次数据下载后的新数据。

The carousel 'move' and positionning me at my previous position (index) even if I want to be position on position (index) 0 at every data download轮播“移动”并将我定位在我之前的 position(索引),即使我想在每次数据下载时成为 position(索引)0 上的 position

How can I refresh correctly my carouselview?如何正确刷新轮播视图?

Here is my code cs:这是我的代码 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);

    }

Thanks in advance提前致谢

The carousel 'move' and positionning me at my previous position (index) even if I want to be position on position (index) 0 at every data download轮播“移动”并将我定位在我之前的 position(索引),即使我想在每次数据下载时成为 position(索引)0 上的 position

According to your description, you download data and display CarouselView, then you swap item for CarouselView in any position. You want to display new data and go to position 0 when download data again, am I right?根据你的描述,你下载数据显示CarouselView,然后你在position中的任意一个item换成CarouselView。你想显示新数据和go到position 0再下载数据,我说的对吗?

If yes, I suggest you can use observablecollection to replace List firstly, it implement INotifyPropertychanged interface to notify when data update.如果是的话,我建议你可以先用observablecollection来代替List,它实现了INotifyPropertychanged接口来通知数据更新的时候。

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

then clear data when you download data again,然后再次下载数据时清除数据,

 newsObj.Clear();

Using ScrollTo scrolls the item at the specified index into view.使用 ScrollTo 将指定索引处的项目滚动到视图中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM