Recently I’ve created a weather application for windows phone 7 using C#. But it can show the temperature only in Celsius but I want to create a button in settings.xaml page that has an option to select either Celsius or Fahrenheit(At present i haven’t created any button for this, the app will automatically shows the temperature in Celsius). Can anybody help me with this??? Thanks in advance for your hard work.
Below is the code i have used for it-:
private void ForecastDownloaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Result == null || e.Error != null)
{
MessageBox.Show("Cannot load Weather Forecast!");
}
else
{
XDocument document = XDocument.Parse(e.Result);
var data1 = from query in document.Descendants("current_condition")
select new Forecast
{
observation_time = (string) query.Element("observation_time"),
temp_C = (string)query.Element("temp_C"),
temp_F = (string)query.Element("temp_F"),
weatherIconUrl = (string)query.Element("weatherIconUrl"),
weatherDesc = (string)query.Element("weatherDesc"),
humidity = (string)query.Element("humidity"),
windspeedMiles = (string)query.Element("windspeedMiles"),
windspeedKmph = (string)query.Element("windspeedKmph")
};
Forecast forecast = data1.ToList<Forecast>()[0];
var data2 = from query in document.Descendants("weather")
select new Forecast
{
date = (string)query.Element("date"),
tempMaxC = (string)query.Element("tempMaxC"),
tempMaxF = (string)query.Element("tempMaxF"),
tempMinC = (string)query.Element("tempMinC"),
tempMinF = (string)query.Element("tempMinF"),
weatherIconUrl = (string)query.Element("weatherIconUrl"),
};
List<Forecast> forecasts = data2.ToList<Forecast>();
for (int i = 0; i < forecasts.Count(); i++)
{
forecasts[i].date = DateTime.Parse(forecasts[i].date).ToString("dddd");
}
AddPanoramaItem(forecast,forecasts);
}
}
private void AddPanoramaItem(Forecast forecast, List<Forecast> forecasts)
{
PanoramaItemObject pio = new PanoramaItemObject();
pio.temperature = "Temperature: " + forecast.temp_C + " °C";
pio.observation_time = "Observ. Time: " + forecast.observation_time;
pio.windspeed = "Wind Speed: " + forecast.windspeedKmph + " Kmph";
pio.huminity = "Huminity: " + forecast.humidity + " %";
pio.weatherIconUrl = forecast.weatherIconUrl;
pio.forecasts = forecasts;
PanoramaItem panoramaItem = new PanoramaItem();
panoramaItem.Header = queries[query];
int index = queries[query].IndexOf(",");
if (index != -1) panoramaItem.Header =
queries[query].Substring(0, queries[query].IndexOf(","));
else panoramaItem.Header = queries[query];
panoramaItem.ContentTemplate =
(DataTemplate)Application.Current.Resources["ForecastTemplate"];
panoramaItem.Content = pio;
Panorama.Items.Add(panoramaItem);
query++;
if (query < queries.Count())
LoadForecast();
}
Use this formula
for Celcius to Farenhite
From Farenhite to Celcius
where
Tc = temperature in degrees Celsius,Tf = temperature in degrees Fahrenheit