Search code examples
c#silverlightwindows-phone-7xamlivalueconverter

The Resource 'ImageConverter' could not be resolved


I'm new on Windows phone and silverlight development. During my learning exercises i have encountered an error which is i mentioned on the title of this post.

My main goal is saving and retrieving the image file to the SQLCE database, and i have used this tutorial http://antonswanevelder.com/2011/10/28/writing-an-image-to-sql-ce-linq-to-sql/

However, i had a problem with this code snippet

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

My idea is compiler cannot locate the resouce ImageConverter. I really need a help on this.

My code is: 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();
    }
}

Solution

  • Let's consider your value converter locates in ProjectName.Converters namespace.

    In xaml you need to add a namespace:

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

    and in Resources tag:

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

    And small tutorial to make you more familiar with IValueConverter here