我目前正在尝试在我的 Windows 8 应用程序中显示图像。List<string>我有一个方法,它用许多图像路径填充类型的属性。我希望在屏幕上显示这些图像。
因此,我实现了一个从字符串到图像的转换器。它正确进入转换器,在Convert方法中命中断点,但屏幕上仍然没有出现任何内容。
这是我的转换器的代码:
namespace TestApp.Converters
{
    public sealed class StringToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, string path)
        {
            try
            {
                return new BitmapImage(new Uri((string)value));
            }
            catch
            {
                return new BitmapImage();
            }
        }
        public object ConvertBack(object value, Type targetType,
                                  object parameter, string path)
        {
            throw new NotImplementedException();
        }
    }
}
从我的 XAML 文件:
    <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>
...
List<string>图像路径的 被调用并且test位于 xaml 文件后面的代码中。
这是我在 xaml.cs 文件后面的代码中相关代码的简单版本:
    public List<string> test { get; set; }
    public MainPage()
    {
        test = new List<string>();
        test.Add("C:\\Users\user\\Pictures\\test.jpg");
        this.InitializeComponent();
    }
非常感谢您对此提供的所有帮助:)