0

This code is in the code behind:

 protected override async void OnAppearing()
    {
        base.OnAppearing();

        listView.ItemsSource = await Service.DataAsync();
    }

The class that fetches data:

public class Service
{
    public List<TodoItem> TodoList { get; private set; }
    static HttpClient client;

    Service()
    {
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
    }

    public async Task<List<TodoItem>> DataAsync()
    {

        TodoList = new List<TodoItem>();


        var uri = new Uri(string.Format(Constants.RestUrl, string.Empty));

        try
        {
            var response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                TodoList = JsonConvert.DeserializeObject<List<TodoItem>>(content);
                Debug.WriteLine(content);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"ERROR {0}", ex.Message);
        }

        return TodoList;
    }
}

When the class and task is static Im able to make this call and populate the listview but not without static. I dont want it to be static. Not duplcate.

KalleP
  • 279
  • 2
  • 13
  • What is the code behind DataAsync? – Woj Nov 29 '18 at 12:02
  • HttpClientRequest. @Woj – KalleP Nov 29 '18 at 12:04
  • This is not code behind. ItemSource is expending to be assigned an array. Is your DataAsync returning an array of items? – Woj Nov 29 '18 at 12:05
  • Updated my question with the class that fetches data. @Woj – KalleP Nov 29 '18 at 12:07
  • The OnAppearing is in the code behind. @Woj – KalleP Nov 29 '18 at 12:08
  • I was able to do this when the class and task that fetches data was static but I dont want it to be static. @Woj – KalleP Nov 29 '18 at 12:10
  • Don't you get an error in DataAsync() method? You should be returning Task and you return just List itself. – Woj Nov 29 '18 at 12:20
  • No in code behind. When I try to call the Service. @Woj – KalleP Nov 29 '18 at 12:20
  • I'm sure this question will be a duplicate, but not with the question currently marked - it's not a NullReferenceException issue as there is no object declared to hold the reference at all, let alone be null. @KalleP - if the DataAsync() method is not marked static (which it isn't) then you must create an instance of the Service class in order to call that method. – Andy Hames Nov 29 '18 at 12:36
  • Hmm, ok can you give me an idea of how to make my class an instance? @AndyHames – KalleP Nov 29 '18 at 12:39
  • All these problems because I cant get the data to show in the list - binding the listview to viewmodel. – KalleP Nov 29 '18 at 12:50
  • Use the new operator - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-operator – Andy Hames Nov 29 '18 at 13:00
  • Do I use that code in the code behind? @AndyHames – KalleP Nov 29 '18 at 13:22
  • Pleas post code that reproduces the error message. – Dour High Arch Nov 29 '18 at 17:06
  • 1
    Issue is calling an instance method as if it were a static method: `listView.ItemsSource = await Service.DataAsync();` So yes, you need to do `listView.ItemsSource = await new Service().DataAsync();` to first create an instance of Service, then call the method on the instance. But it seems you may want a singleton since you said "no duplicates." http://csharpindepth.com/Articles/General/Singleton.aspx . If you do not want duplicates, a static class or a singleton or your main choices. – jgoldberger - MSFT Nov 30 '18 at 01:38

0 Answers0