使用converter-windows phone 8动态设置图像源

时间:2014-02-18 09:36:37

标签: c# xaml windows-phone-8

根据网络服务的响应,我需要绑定我使用转换器的本地文件夹中的两个图像中的任何一个。

 public class typeconverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string text = value.ToString();

            if (text != null)
            {
                if (text == "1")
                {
                    Uri uri = new Uri("/projectname;component/Assets/call.png", UriKind.Relative);

                    return new BitmapImage(uri);
                }
                if (text == "2")
                {
                    BitmapImage imgSource = new BitmapImage(
          new Uri("/projectname;component/Assets/wtp.png", UriKind.RelativeOrAbsolute));

                    return imgSource;
                }

            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

我已将图像作为资源进行构建操作。 在XAML中

<ListBox x:Name="ListBox5" Margin="0,-15,0,-36"
                    Width="400" HorizontalAlignment="Center" SelectionChanged="MainListBox5_SelectionChanged"
                    ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Height="120" Width="420">
                                <Grid MinWidth="420">
                                    <TextBlock FontSize="36" Text="{Binding ticket_number}" Margin="5,0,0,0" MinWidth="300" />
                                    <TextBlock Text="{Binding created_time}" Margin="0,15,0,0" MinWidth="115" HorizontalAlignment="Right" />
                                </Grid>
                                <Grid MinWidth="420">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="30"></ColumnDefinition>
                                        <ColumnDefinition Width="390"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Image Grid.Column="0" Source="{Binding request_type_id,Converter={StaticResource typeconvert}}"></Image>
                                    <TextBlock  Text="completed" Foreground="#FF339933" Margin="5,0,0,0" Grid.Column="1"/>
                                    </Grid>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>


<Grid.Resources>
            <local:typeconverter x:Key="typeconvert"/>
        </Grid.Resources>

我无法显示图像,当我调试时,我可以在imgSource.UriSource的所有属性中找到“System.Invalid operation exception”。

1 个答案:

答案 0 :(得分:1)

public class typeconverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text = value.ToString();

        if (text != null)
        {
            if (text == "1")
            {
               return "/Assets/call.png";                    
            }
            if (text == "2")
            {
               return "/Assets/wtp.png";                   
            }

        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

这可以解决您的问题。