Load weather info at app startup

Hello I'm still begining with xamarin forms so I followed the Weather App example from https://developer.xamarin.com/samples/xamarin-forms/Weather/ I have it working on a different project the problem is that this one works with a Clicked event and I'd like that this weather data is shown on top of the app at my shell TitleView element don't care about being updated regularly or something just one call when you load the app.

The code for the clicked event is
async void OnGetWeatherButtonClicked(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_cityEntry.Text))
{
WeatherData weatherData = await _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint));
BindingContext = weatherData;
}
}

I changed that to be an async task and put that on App.xaml.cs file inside protected override void OnStart() but still the data is not loaded and read about app freezing if data fails to load, how can this be achieved, thanks

Answers

  • Can't say why it wasn't working the first time but it worked as
    protected override void OnStart()
    {
    // Handle when your app starts
    OnGetWeather();
    }

    async void OnGetWeather()
        {
            WeatherData weatherData = await                 _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint));
            BindingContext = weatherData;            
        }
    

    Now I assume is a binding problem

  • Do I need to copy that to every page if I need it to be at shell titleview? it's the only way I got it to work :( tryed pasting in App.xaml.cs also in AppShell and the only way it worked was at my contentpage why is that? is there another option to make it more practical?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    I think you can get the weather data in the App.xaml.cs, then use MessageCenter to send this data, If other pages what to use weather data, it could use messageCenter to subcribe this data. then show then in shell titleview.

  • @LeonLu said:
    I think you can get the weather data in the App.xaml.cs, then use MessageCenter to send this data, If other pages what to use weather data, it could use messageCenter to subcribe this data. then show then in shell titleview.

    I beg you pardon I don't know yet what's the message center nor how to use it in this way :( so far I figured out to put it this way inside of my code... lightly useful any step forward is a progress to me

               public async void GetWeather()
        {
            WeatherData weatherData = await _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint));
            BindingContext = weatherData;
            temperatura.Text = Math.Round(weatherData.Main.Temperature).ToString() + " °C";           
        }
    
  • LeonLuLeonLu Member, Xamarin Team Xamurai
  • Thanks for the time, I'll be checking it out shortly hope this solve many problems

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, waitting for your update.

Sign In or Register to comment.