Herb
Herb

Reputation: 171

How do I use an url+binding in xamarin forms image tag

I'd like to put this string in an image source:

http://openweathermap.org/img/w/10d.png https://openweathermap.org/weather-conditions

But the string has to be something like:

"http://openweathermap.org/img/w/ + {Binding weather} + .png"

The problem I have is that apparently I can't use StringFormat in the label so how can I make a single line of

Other problem I have is that I don't quite understand how the binding for this works perhaps that's the whole problem, that's given from a script like:

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

What can I do to get the icon variable from the Weatherdata, or is there a more practical way to solve this, I still don't know very well where is that allocated or how to put that "bindingcontext" values inside c# thanks

Upvotes: 0

Views: 277

Answers (2)

Herb
Herb

Reputation: 171

My working code turned out to be

     public async void GetWeather()
    {
        WeatherData weatherData = await _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint));
        BindingContext = weatherData;
        temperatura.Text = Math.Round(weatherData.Main.Temperature).ToString() + " °C";
        icon.Source = "http://openweathermap.org/img/w/" + weatherData.Weather[0].Icon + ".png";                       
    }

this way I can see the icons and temperature for my query at openweather https://openweathermap.org/weather-conditions

Upvotes: 0

Jason
Jason

Reputation: 89082

create a property called WeatherIcon in your WeatherData class and bind that as your image source

public string WeatherIcon
{
  get {
    return $"http://openweathermap.org/img/w/{Weather[0].Icon}.png";
  }
}

where weather is the property that contains the string you need to insert into the url

Upvotes: 1

Related Questions