2012-10-27 91 views
4

我是Windows手机和silverlight开发的新手。在我的学习练习中,我遇到了一个错误,这是我在这篇文章的标题中提到的。资源'ImageConverter'无法解析

我的主要目标是保存和检索图像文件到SQLCE数据库,我已经使用这个教程http://antonswanevelder.com/2011/10/28/writing-an-image-to-sql-ce-linq-to-sql/

然而,我不得不用这个代码片断

<Image Source="{Binding ItemImage, Converter={StaticResource ImageConverter}}" Stretch="UniformToFill"/>

一个问题我想法是编译器无法找到资源ImageConverter。我真的需要帮助。

我的代码是: MainPage.xaml中

<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="CallListListBoxItemTemplate"> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock Text="{Binding CallName}" Foreground="DarkCyan" FontSize="{StaticResource PhoneFontSizeLarge}" 
         VerticalAlignment="Top" Margin="12,12,0,0"/> 
     </StackPanel> 
    </DataTemplate> 
    <DataTemplate x:Key="PersonalInfoListBoxItemTemplate"> 
     <Grid > 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="Auto"/> 
      </Grid.ColumnDefinitions> 
      <Image Source="{Binding PersonImage, Converter={StaticResource ImageConverters}}" Stretch="UniformToFill" Name="_personPhoto" /> 

MainPage.xaml.cs中

public class ImageConverters : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is byte[]) 
     { 
      MemoryStream ms = new MemoryStream(value as byte[]); 
      WriteableBitmap wb = PictureDecoder.DecodeJpeg(ms, 100, 100); 
      return wb; 
     } 
     else 
     { 
      return null; 
     } 

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

回答

7

让我们考虑你的价值转换器ProjectName.Converters命名空间所在。

XAML中,你需要添加一个命名空间:

<phone.PhoneApplicatinPage 
     .. all your code here 
     xmlns:converters="clr-namespace;ProjectName.Converters" 
     > 

,并在资源标签:

<phone:PhoneApplicationPage.Resources> 
    <converters:ImageConverters x:Key="ImageConverter"/> 
     <!- your DataTemplates here--> 

和小教程,让你更熟悉IValueConverterhere

+0

谢谢,这真的帮助。 –