il problema è nell'await.
cioè, questo non funziona:
codice:
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
var mockItems = new List<Item>
{
new Item { Id = Guid.NewGuid().ToString(), Title = "100", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "second item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Third item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Fourth item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Fifth item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Sixth item", Author="This is an item description." },
};
var client = new HttpClient();
var json = await client.GetStringAsync(Costanti.allBooks);
//var mockItems = JsonConvert.DeserializeObject<List<Item>>(json);
items = mockItems;
return await Task.FromResult(items);
}
mentre questo funziona (anche se poi nella realtà il json remoto non lo uso):
codice:
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
var mockItems = new List<Item>
{
new Item { Id = Guid.NewGuid().ToString(), Title = "100", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "second item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Third item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Fourth item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Fifth item", Author="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Title = "Sixth item", Author="This is an item description." },
};
var client = new HttpClient();
var json = client.GetStringAsync(Costanti.allBooks);
//var mockItems = JsonConvert.DeserializeObject<List<Item>>(json);
items = mockItems;
return await Task.FromResult(items);
}
il problema è che mi da errore se lo levo e provo ad usare i dati remoti:
codice:
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
var client = new HttpClient();
var json = client.GetStringAsync(Costanti.allBooks);
var mockItems = JsonConvert.DeserializeObject<List<Item>>(json);
items = mockItems;
return await Task.FromResult(items);
}
ottengo questo errore:
codice:
MockDataStore.cs(71,71): Error CS1503: Argument 1: cannot convert from 'System.Threading.Tasks.Task<string>' to 'string' (CS1503)