Xamarin tvOS:Collectionview未显示REST API响应中的数据?
我在下面的博客中介绍了如何在我的tvOS应用程序中实现Xamarin tvOS:Collectionview未显示REST API响应中的数据?,xamarin,uicollectionview,tvos,Xamarin,Uicollectionview,Tvos,我在下面的博客中介绍了如何在我的tvOS应用程序中实现Collectionview。在本博客中,数据添加到CityViewDatasource类中。静态数据添加到此博客上,如下所示: Cities.Clear(); Cities.Add(new CityInfo("City01.jpg", "Houses by Water", false)); Cities.Add(new CityInfo("Ci
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));
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);
}
我需要通过调用REST API将静态数据更改为动态数据,如下所示:
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));
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);
}
试试看
{
HttpClient=新的HttpClient();
var response=await client.GetAsync(“我的RESTAPI”);
if(响应。IsSuccessStatusCode)
{
var Response=JsonConvert.DeserializeObject(wait Response.Content.ReadAsStringAsync());
如果(!string.IsNullOrWhiteSpace(Response.ToString()))
{
var category=JsonConvert.DeserializeObject(Response.ToString());
城市。清除();
对于(int i=0;i>”+e);
}
但是当我运行项目时,UI上没有数据,只显示一个空白屏幕。输出窗口中未显示任何错误
我已将服务域包括在info.plist的
NSAppTransportSecurity
下。RESTAPI调用成功,我能够获得项目的数量。我添加了一个示例项目供参考。检查共享代码和项目后,您需要在收到后重新加载数据
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();
});
}
}
...
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);
}
...
您可以按如下方式修改CityCollectionViewCell.cs
:
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();
});
}
}
...
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);
}
...
你能看一下for循环中被注释的第一行吗。我需要使用这一行而不是第二行,项目有图像url,我需要在UI上显示特定的图像。我现在取消注释该行,但图像部分为空。通过URL加载图像是否还有其他附加内容?@SreejithSree嗨,动态图像意味着像gif一样的图像?响应中有一个imageurl,我们需要添加根URL以获得完整的图像,如果您查看注释行,您可以看到根URL+图像剩余的URL。我需要在UI上显示该图像(不是GIF唯一图像),示例图像URL:@ SReijthsRe得到它,您修改了