2014-02-18 37 views
0

基于Web服务的响应,我需要绑定来自我使用转换器的本地文件夹中的两个图像。使用转换器-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操作异常”。

+0

您发布的xaml代码不足以理解该问题。 –

+0

@MuhammadUmar我已经添加了列表框模板.. –

+0

您是否试图调试您的代码?在Convert()中设置断点并查看这些值是否正确? – Romasz

回答

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; 
    } 
} 

这可能会解决您的问题。