【问题标题】:tvOS: Collectionview is not showing data from the REST API response?tvOS:Collectionview 不显示来自 REST API 响应的数据?
【发布时间】:2020-06-29 10:17:25
【问题描述】:

我正在关注 this 博客,以在我的 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 上没有数据,只显示一个空白屏幕。输出窗口中未显示任何错误。

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

【问题讨论】:

    标签: xamarin uicollectionview tvos


    【解决方案1】:

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

    【讨论】:

    • 能否请您查看 for 循环中注释的第一行。我需要使用该行而不是第二行,这些项目具有图像 url,我需要在 UI 上显示该特定图像。我现在取消注释该行,但图像部分是空白的。通过 URL 加载图像还有其他附加功能吗?
    • @SreejithSree 嗨,动态图像是指像 gif 的图像?
    • 响应中有一个imageurl,我们需要添加rooturl来获取完整的图像,如果你检查注释行你可以看到一个root url + 图片剩余url。我需要显示UI 上的图像(不是仅 gif 图像),示例图像 url:s3.us-east-1.amazonaws.com/catholic-brain/prod/dc/cbrain-app/…
    • @SreejithSree 知道了,你修改CityCollectionViewCell.cs文件添加显示url图片的方法。然后它会显示。
    • 我已经通过用 %20 替换空格解决了这个问题,现在我可以在 UI 上查看我的图像了。 drive.google.com/file/d/1zPS91TvmHDoPA-sCGKmesRsxThk_Zxpo/…非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多