我正在try 从Unsplash获取一个API来显示图像
然而,我无法显示它们,因为错误CS1579声明:Getting error foreach statement cannot operating on type Root的变量,因为根不包含一个公共定义的'GetEnumerator'.
using Newtonsoft.Json;
namespace ImageAPI
{
public partial class MainPage : ContentPage
{
int count = 0;
private List<String> lst;
private object usrName;
public MainPage()
{
InitializeComponent();
lst = new List<String>();
BindingContext = this;
}
private async void OnCounterClicked(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
var url = "https://api.unsplash.com/search/photos/?page=1&query=office&client_id=R62b2dZL9UoHcYhxook8WAkwklTUcBHqhJEmxe5AEqY";
try
{
HttpResponseMessage response = await client.GetAsync(url);
var content = await response.Content.ReadAsStringAsync();
var apiResponse = JsonConvert.DeserializeObject<Root>(content);
if (response.IsSuccessStatusCode)
{
if (apiResponse != null) {
foreach (var photo in apiResponse)
{
var username = photo.User?.Username;
lst.Add(username);
}
imageListView.ItemsSource = lst;
}
}
else
{
await DisplayAlert("Error", "Invalid response", "Ok");
}
}
catch (Exception ex)
{
await DisplayAlert("Exception", $"{ex.Message}", "Ok");
}
}
}
}
列表视图因此没有更新,没有显示任何内容!
感谢帮助!