资源“ImageConverter”无法解析。

5
我是新手,正在学习Windows手机和银光开发。在我的练习中,我遇到了一个错误,就是本帖标题中提到的问题。
我的主要目标是将图像文件保存和检索到SQLCE数据库中,并使用以下教程:http://antonswanevelder.com/2011/10/28/writing-an-image-to-sql-ce-linq-to-sql/ 然而,我在这段代码中遇到了问题: 我的想法是编译器无法找到资源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();
    }
}
1个回答

8

让我们考虑你的值转换器位于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-->

这是一个小教程,让你更加熟悉 IValueConverter。你可以在这里了解更多相关信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接