How to fix a CS0029 error in .NET MAUI app

wire_jp 216 Reputation points
2023-06-17T19:32:33.65+00:00

I have a CS0029 error in the WeatherViewModel.cs file a .NET MAUI app, which references the NetworkManager.cs. The NetworkManager.cs file and WeatherViewModel.cs files are shown below: -

The CS0029 error is: -

CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<WeatherMap.Models.Weather.CurrentWeather>' to 'WeatherMapModels.Weather.CurrentWeather'

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

NetworkManager.cs file

NetworkManager.cs file

public class NetworkManager
    {
        public async Task<CurrentWeather> GetCurrentWeather(int id)
        {
            return await Task<CurrentWeather>.Factory.StartNew(() =>
            {
                string url = $"http://api.openweathermap.org/data/2.5/weather?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();
                }
                reader.Close();

                return JsonConvert.DeserializeObject<CurrentWeather>(responseJson);
            });
        }
      }    

    }

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);  // CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<WeatherMap.Models.Weather.CurrentWeather>' to 'WeatherMapModels.Weather.CurrentWeather'
                currentWeather.City = city;
                Forecast = networkManager.GetForecast(city.Id).List; 
                BackgroundSource = currentWeather.Weather[0].Icon;

                MainThread.BeginInvokeOnMainThread(() =>
                {
                    ViewModels[0].Init(currentWeather);
                    ViewModels[1].Init(Forecast);
                });
            }
            catch { }

        }

  

Developer technologies .NET .NET MAUI
Developer technologies .NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. wire_jp 216 Reputation points
    2023-06-22T01:36:58.58+00:00

    I tried the following and I am checking with you if this will work: -

    NetworkManager.cs file
    
    public class NetworkManager
        {
            public CurrentWeather GetCurrentWeather(int id)
            {
               
                   string url = $"http://api.openweathermap.org/data/2.5/weather?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();
                    }
                    reader.Close();
    
                    return JsonConvert.DeserializeObject<CurrentWeather>(responseJson);
                });
            }
          }    
    
        }
    
    1 person found this answer helpful.

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.