2012-04-22 61 views
0

我試圖根據天氣WCF在預測中分配給當天的天氣ID顯示天氣圖標的圖像。我將數據顯示在數據網格中,並希望添加一個帶有圖標的新列。不幸的是,圖標是我知道的GIF Silverlight不支持,所以如果有人可以幫助這將是偉大的!如何使用WCF服務在Silverlight數據網格中顯示GIF圖像?

public partial class MainPage : UserControl 
{ 

創建與它們相關聯的圖像和ID的數組。

private WeatherDescription[] weatherInformation; 

    WeatherSoapClient weatherClient = new WeatherSoapClient(); 
    public MainPage() 
    { 
     InitializeComponent(); 

     weatherClient.GetCityForecastByZIPCompleted += new EventHandler<GetCityForecastByZIPCompletedEventArgs>(weatherClient_GetCityForecastByZIPCompleted); 
     weatherClient.GetCityWeatherByZIPCompleted += new EventHandler<GetCityWeatherByZIPCompletedEventArgs>(weatherClient_GetCityWeatherByZIPCompleted); 
     weatherClient.GetWeatherInformationCompleted += new EventHandler<GetWeatherInformationCompletedEventArgs>(weatherClient_GetWeatherInformationCompleted); 
     weatherClient.GetWeatherInformationAsync(); 
    } 

這就是我將WCF調用的結果設置爲數組的地方。

void weatherClient_GetWeatherInformationCompleted(object sender, GetWeatherInformationCompletedEventArgs e) 
    { 
     weatherInformation = e.Result; 
    } 

    void weatherClient_GetCityForecastByZIPCompleted(object sender, GetCityForecastByZIPCompletedEventArgs e) 
    { 
     this.dataGrid1.ItemsSource = e.Result.ForecastResult; 
     for (int i = 0; i < e.Result.ForecastResult.Length; i++) 
     { 
      if (i == 0) 
      { 
       e.Result.ForecastResult[i].Temperatures.DaytimeHigh += "°F"; 
       e.Result.ForecastResult[i].ProbabilityOfPrecipiation.Daytime += "%"; 
      } 
      else 
      { 
       e.Result.ForecastResult[i].Temperatures.DaytimeHigh += "°F"; 
       e.Result.ForecastResult[i].Temperatures.MorningLow += "°F"; 
       e.Result.ForecastResult[i].ProbabilityOfPrecipiation.Daytime += "%"; 
       e.Result.ForecastResult[i].ProbabilityOfPrecipiation.Nighttime += "%"; 
      } 
     } 
    } 

    void weatherClient_GetCityWeatherByZIPCompleted(object sender, GetCityWeatherByZIPCompletedEventArgs e) 
    { 
     this.textBlock1.Text = "City: " + e.Result.City + "\n"; 
     this.textBlock1.Text += "State: " + e.Result.State + "\n"; 
     this.textBlock1.Text += "Weather ID: " + e.Result.WeatherID + "\n"; 
     this.textBlock1.Text += "Weather Station: " + e.Result.WeatherStationCity + "\n"; 
     this.textBlock1.Text += "Temperature: " + e.Result.Temperature + "°F \n"; 
     this.textBlock1.Text += "Description: " + e.Result.Description + "\n"; 
     this.textBlock1.Text += "Pressure: " + e.Result.Pressure + "\n"; 
     this.textBlock1.Text += "Relative Humidity: " + e.Result.RelativeHumidity + "% \n"; 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     weatherClient.GetCityWeatherByZIPAsync(inputZip.Text); 
     weatherClient.GetCityForecastByZIPAsync(inputZip.Text); 
    } 
} 
} 

然後,如果它有幫助,這是我的dataGrid的標記。

<sdk:DataGrid AutoGenerateColumns="False" Height="310" Margin="12,131,407,0" Name="dataGrid1" VerticalAlignment="Top" AlternatingRowBackground="#00010000"> 
     <sdk:DataGrid.Columns> 
      <sdk:DataGridTextColumn Header="Date" Binding="{Binding Date}" /> 
      <sdk:DataGridTextColumn Header="ID" Binding="{Binding WeatherID}" /> 
      <sdk:DataGridTextColumn Header="Description" Binding="{Binding Desciption}" /> 
      <sdk:DataGridTemplateColumn Header="Temperature"> 
       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Temperatures.DaytimeHigh}" Name="DaytimeHigh" /> 
          <TextBlock Text="{Binding Temperatures.MorningLow}" Name="MorningLow"/> 
         </StackPanel> 
        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
      </sdk:DataGridTemplateColumn> 
      <sdk:DataGridTemplateColumn Header="Probability of Precipitation"> 
       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding ProbabilityOfPrecipiation.Daytime}" Name="Daytime"/> 
          <TextBlock Text="{Binding ProbabilityOfPrecipiation.Nighttime}" Name="Nighttime"/> 
         </StackPanel> 
        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
      </sdk:DataGridTemplateColumn> 
     </sdk:DataGrid.Columns> 
    </sdk:DataGrid> 

如果您需要更多的信息,請告訴我。我希望有人能夠提供幫助。

+0

那麼,你的問題是什麼?是關於GIF或您的實施 – KodeKreachor 2012-04-22 00:50:58

+0

我不確定如何去做。第一個問題是在引用ID時在數據網格中顯示圖像。然後如何將gif轉換成JPG格式? – Fogolicious 2012-04-22 01:06:49

回答

0

Silverlight不支持GIF,因此您必須將它們轉換爲jpg或png。我建議你只做一次服務器端。

然後,您可以使用相對路徑在模板列中的數據網格中顯示圖像。

<Image x:Name="myImage" Source="../someImage.jpg" /> 

你的圖像存儲在哪裏?你提到通過id來引用它們。它們是否存儲在數據庫中?然後,您可以編寫一個HTTP處理程序來加載圖像或通過您的服務加載它們。

+0

我其實只是寫了一個更好的嘗試和幫助解決這個問題,所以這是這個鏈接。 http://stackoverflow.com/questions/10269731/creating-a-converter-to-take-an-id-and-create-an-image-in-silverlight 如果這不能回答你的問題,讓我知道。我感謝幫助! – Fogolicious 2012-04-22 16:26:49