我正在关注这个博客,以便在我的tvOS应用程序中实现Collectionview
。在此博客中,数据将添加到CityViewDatasource
类上。静态数据在此博客中添加,如下所示:
Cities.Clear();
Cities.Add(new CityInfo("City01.jpg", "Houses by Water", false));
Cities.Add(new CityInfo("City02.jpg", "Turning Circle", true));
Cities.Add(new CityInfo("City03.jpg", "Skyline at Night", true));
Cities.Add(new CityInfo("City04.jpg", "Golden Gate Bridge", true));
Cities.Add(new CityInfo("City05.jpg", "Roads by Night", true));
Cities.Add(new CityInfo("City06.jpg", "Church Domes", true));
Cities.Add(new CityInfo("City07.jpg", "Mountain Lights", true));
Cities.Add(new CityInfo("City08.jpg", "City Scene", false));
我需要通过调用 REST API 将静态数据更改为动态数据,如下所示:
try
{
HttpClient client = new HttpClient();
var response = await client.GetAsync("My REST API");
if (response.IsSuccessStatusCode)
{
var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
if (!string.IsNullOrWhiteSpace(Response.ToString()))
{
var category = JsonConvert.DeserializeObject<VideoList>(Response.ToString());
Cities.Clear();
for (int i = 0; i < category.webContentHBList.Count; i++)
{
Cities.Add(new CityInfo(category.webContentHBList[i].ImageUrl, category.webContentHBList[i].title, true));
}
}
}
}
catch (Exception e)
{
Debug.WriteLine("exception:>>" + e);
}
但是当我运行项目时,UI 上没有数据,只显示一个空白屏幕。输出窗口中未显示任何错误。
我已经在 info.plist 下包含了NSAppTransportSecurity
下的服务域。REST API 调用成功,我能够获取项目计数。我在这里添加了一个示例项目以供参考。
检查完共享代码和项目后,需要ReloadData
后接收 API数据并处理它们。
修改如下:
HttpClient client = new HttpClient();
var response = await client.GetAsync("REST API");
if (response.IsSuccessStatusCode)
{
var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
if (!string.IsNullOrWhiteSpace(Response.ToString()))
{
var category = JsonConvert.DeserializeObject<VideoList>(Response.ToString());
Cities.Clear();
for (int i = 0; i < category.webContentHBList.Count; i++)
{
//Cities.Add(new CityInfo("category.webContentHBList[i].thumbnailImageUrl.Replace(" ","%20"), category.webContentHBList[i].pageTitle, true));
Cities.Add(new CityInfo("City01.jpg", category.webContentHBList[i].pageTitle, true));
}
// reload data after receiving
InvokeOnMainThread(() => {
// manipulate UI controls
this.ViewController.ReloadData();
});
}
}
====
=====================更新=======================================如果要显示网址图像:
Cities.Add(new CityInfo(category.webContentHBList[i].thumbnailImageUrl, category.webContentHBList[i].pageTitle, true));
您可以按如下方式修改CityCollectionViewCell.cs
:
...
public CityInfo City {
get { return _city; }
set {
_city = value;
//CityView.Image = UIImage.FromFile (City.ImageFilename);
CityView.Image = FromUrl(City.ImageFilename);
// here api url not showing , it's invalid in tvOS
// such as using test url , it will show
// CityView.Image = FromUrl(https://s1.ax1x.com/2020/06/30/N5sh5R.png)
CityView.Alpha = (City.CanSelect) ? 1.0f : 0.5f;
CityTitle.Text = City.Title;
}
}
#endregion
static UIImage FromUrl(string uri)
{
using (var url = new NSUrl(uri))
using (var data = NSData.FromUrl(url))
return UIImage.LoadFromData(data);
}
...