简体   繁体   中英

Implementing IValueConverter to convert string to image

I am currently trying to displaying images in my Windows 8 application. I have a method which populates a property of type List<string> with a number of paths to images. I wish to display these images on screen.

Thus, I have implemented a converter to go from string to image. However, I get the errors :

  • The name "StringToImageConverter" does not exist in the namespace "using:TestApp.Converters".
  • 'TestApp.Converters.StringToImageConverter' does not implement interface member 'Windows.UI.Xaml.Data.IValueConverter.ConvertBack(object, System.Type, object, string)'
  • 'TestApp.Converters.StringToImageConverter' does not implement interface member 'Windows.UI.Xaml.Data.IValueConverter.Convert(object, System.Type, object, string)'

Here is the code from my Converter :

namespace TestApp.Converters
{
    public sealed class StringToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            try
            {
                return new BitmapImage(new Uri((string)value));
            }
            catch
            {
                return new BitmapImage();
            }
        }

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

And from my XAML file :

    <common:LayoutAwarePage
        ...
        xmlns:converters="using:TestApp.Converters"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
        <Page.Resources>
            <converters:StringToImageConverter x:Key="StringToImageConverter"> </converters:StringToImageConverter>
        </Page.Resources>
...
  <ItemsControl ItemsSource="{Binding Path=test}" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="4"
              HorizontalContentAlignment="Stretch">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Converter={StaticResource StringToImageConverter}}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
...

Should this work for displaying my images in the Windows 8 application? The List<string> of image paths is called test and is in the code behind of the xaml file.

Thanks very much for any and all help with this :)

Apparently there are two types of IValueConverters:

Windows.UI.Xaml.Data.IValueConverter
System.Windows.Data.IValueConverter

It sounds like your framework is expecting the former, while you're implementing the latter.

You probably also need to change this:

xmlns:converters="using:TestApp.Converters"

to this:

xmlns:converters="clr-namespace:TestApp.Converters"

Windows.UI.Xaml.Data.IValueConverter期望最后一个参数是string ,而不是CultureInfo

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
        xmlns:s="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:p="clr-namespace:System;assembly=mscorlib"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:Entities="clr-namespace:Entities;assembly=Entities" 
        mc:Ignorable="d" 
        x:Name="XXXXX"
        x:Class="AAAA.XXXXX" Title="Seciones" Height="644.305" Width="909.579"
        xmlns:c="clr-namespace:AAAA">
    <Window.Resources>
        <c:StringToImageConverter x:Key="stringToImageConverter"/>
    </Window.Resources>
.....
</Window>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM