5

Windows Phone と Silverlight の開発は初めてです。学習演習中に、この投稿のタイトルに記載されているエラーに遭遇しました。

私の主な目標は、イメージ ファイルを SQLCE データベースに保存して取得することです。 -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();
    }
}
4

1 に答える 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 ここに慣れるための小さなチュートリアル

于 2012-10-27T15:52:20.957 に答える