【问题标题】:How to show GIF images in a Silverlight dataGrid using WCF services?如何使用 WCF 服务在 Silverlight 数据网格中显示 GIF 图像?
【发布时间】:2012-05-03 02:22:36
【问题描述】:

我正在尝试根据天气 WCF 在预测中分配给当天的天气 ID 显示天气图标的图像。我正在数据网格中显示数据,并希望添加一个带有图标的新列。不幸的是,这些图标是我知道 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>

如果您需要有关它的更多信息,请告诉我。我希望有人能够提供帮助。

【问题讨论】:

  • 那么,您的问题是什么?是关于 gif 还是你的实现
  • 我不确定该怎么做。第一个问题是在引用 ID 时在数据网格中显示图像。然后如何将 gif 转换为 jpg?

标签: c# wcf silverlight xaml gif


【解决方案1】:

Silverlight 不支持 GIF,因此您必须将它们转换为 jpg 或 png。我建议你只做一次服务器端。

然后您可以使用相对路径在模板列中的数据网格中显示您的图像。

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

您的图像存储在哪里?您提到通过 id 引用它们。它们是否存储在数据库中?然后,您可以编写一个 HTTP 处理程序来加载图像或通过您的服务加载它们。

【讨论】:

  • 我实际上只是写了一个更好的尝试和帮助解决这个问题,所以这是链接。 stackoverflow.com/questions/10269731/… 如果这不能回答您的问题,请告诉我。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-09-16
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2010-11-30
  • 2016-09-16
相关资源
最近更新 更多