【发布时间】: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