Search code examples
xamarinuicollectionviewtvos

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


I am following this blog for implementing the Collectionview in my tvOS application. In this blog the data is adding on the CityViewDatasource class. Static datas are added on this blog like below:

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

I need to change the static data into dynamic by calling a REST API like below:

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

But when I run the project no data is coming on the UI, only a blank screen is showing. No errors are showing in the output window.

I have included the service domains on the info.plist under NSAppTransportSecurity. The REST API calling is success and I able to get the count of items. I have added a sample project here for the reference.


Solution

  • After check the shared code and project ,you need to ReloadData after receiving api data and dealing with them .

    Modify as follow :

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

    =======================Update=================================

    If want to show url image :

    Cities.Add(new CityInfo(category.webContentHBList[i].thumbnailImageUrl, category.webContentHBList[i].pageTitle, true));
    

    You can modify CityCollectionViewCell.cs as follow :

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