2012-10-27 52 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

謝謝,這真的幫助。 –

相關問題