0

最近我使用 C# 为 windows phone 7 创建了一个天气应用程序。但它只能以摄氏度显示温度,但我想在 settings.xaml 页面中创建一个按钮,该按钮可以选择摄氏度或华氏度(目前我还没有为此创建任何按钮,应用程序将自动显示摄氏温度)。有人可以帮我吗???提前感谢您的辛勤工作。

以下是我使用的代码-:

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();
}
4

1 回答 1

2

使用这个公式

为 Celcius 到 Farenhite

Tc = 5 (Tf-32) /9;

从 Farenhite 到 Celcius

Tf = (9/5)*Tc+32

其中Tc = temperature in degrees Celsius,Tf = temperature in degrees Fahrenheit

于 2012-05-20T10:01:12.223 回答