tvOS:Collectionview 未显示来自 REST API 响应的数据?

tvOS: Collectionview is not showing data from the REST API response?

我正在关注 this 博客以在我的 tvOS 应用程序中实现 Collectionview。在这个博客中数据正在添加 CityViewDatasource class。本博客添加静态数据如下:

        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 上没有数据时,只显示一个空白屏幕。输出 window.

中未显示任何错误

我已将 info.plist 的服务域包含在 NSAppTransportSecurity 下。 REST API 调用成功,我能够获取项目数。我添加了一个示例项目 here 以供参考。

查看共享代码和项目后,需要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();
        });
    }
}

=======================更新===================== ============

如果要显示 url 图片:

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);
}

...