How to fix CS1061 in .NET MAUI app

wire_jp 216 Reputation points
2023-06-17T19:41:58.4966667+00:00

I have a CS1061 error in the WeatherViewModel.cs file a .NET MAUI app, which references the NetworkManager.cs. The error is: -

CS1061 'Task<Forecast>' does not contain a definition for 'List' and no accessible extension method 'List' accepting a first argument of type 'Task<Forecast>' could be found (are you missing a using directive or an assembly reference?)

The NetworkManager.cs file and the WeatherViewModel.cs files are shown below: -

NetworkManager.cs file


   
public class NetworkManager    
{        
public Forecast GetForecast(int id)
        {
            string url = $"http://api.openweathermap.org/data/2.5/forecast?id={id}&units=metric&appid=921e83b9da8a40a760ad74d5cedd6bbd";
            using var httpClient = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://api.openweathermap.org/data/2.5/weather?id={id}&units=metric&appid=921e83b9da8a40a760ad74d5cedd6bbd");
            string responseJson = string.Empty;
            var response = httpClient.Send(request);
            using var reader = new StreamReader(response.Content.ReadAsStream());
            var responseBody = reader.ReadToEnd();
            using (StreamReader streamReader = new StreamReader(responseBody))
            {
                responseJson = streamReader.ReadToEnd();
            }

            Forecast list = JsonConvert.DeserializeObject<Forecast>(responseJson);

            reader.Close();
            return list;
        }
   }
}

WeatherViewModel.cs file

WeatherViewModel.cs
public WeatherViewModel(City city)   
 {

        networkManager = new NetworkManager();

        ViewModels = new ObservableCollection<IViewModel>() { new DayViewModel(), new WeekViewModel() };

        try

        {

            CurrentWeather currentWeather = networkManager.GetCurrentWeather(city.Id); 

            currentWeather.City = city;

            Forecast = networkManager.GetForecast(city.Id).List; // CS1061 'Task<Forecast>' does not contain a definition for 'List' and no accessible extension method 'List' accepting a first argument of type 'Task<Forecast>' could be found (are you missing a using directive or an assembly reference?)

            BackgroundSource = currentWeather.Weather[0].Icon;

            MainThread.BeginInvokeOnMainThread(() =>

            {

                ViewModels[0].Init(currentWeather);

                ViewModels[1].Init(Forecast);

            });

        }

        catch { }

    }

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-06-19T03:00:16.6033333+00:00

    Hello,

    A detailed error resolution of this error code is given in the official C# documentation.

    'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?). This error occurs when you try to call a method or access a class member that does not exist.

    Please refer to Compiler Error CS1061 for more details.

    CS1061 'Task<Forecast>' does not contain a definition for 'List' and no accessible extension method 'List' accepting a first argument of type 'Task<Forecast>' could be found (are you missing a using directive or an assembly reference?)

    According to the above error message you provided, Task<Forecast> is not a collection class, so you cannot call List to return a list.

    You could replace networkManager.GetForecast(city.Id).List; with networkManager.GetForecast(city.Id).Result;.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.