0

我正在将一个可观察的集合绑定到 UWP 项目中的网格,并且绑定适用于字符串数据类型,但不适用于 ImageSource 数据类型。图像需要从本地文件夹加载。图像通过其他部分的应用程序下载到本地文件夹[代码不包含在这里]但是下载部分工作正常

下面的代码用于创建要绑定的集合

  public class LibraryBookModel
  {
    public string Name { get; set; }
    public string Title { get; set; }       
    public ImageSource CoverImage { get; set; }
  }   
   private ObservableCollection<LibraryBookModel> DPMBooks;
   private async Task<bool> lodLibrariesAsync(Uri library_url)
    {
        try
        {
            //create a view model object and add to observable collection
            LibraryBookModel view_model = new LibraryBookModel {  Name = "Myname", Title = "Myttile" };
            string filename =  "picture_uname.png"; //this file is present in the folder                                
            string cover_path = string.Format("{0}\\{1}", COVERPICS_Folder, filename);
            view_model.CoverImage = new BitmapImage(new Uri(new Uri("ms-appdata:///"), cover_path));
            DPMBooks.Add(view_model);
            return true;
        }
        catch
        {
            return false;
        }
    }
    const string COVERPICS_Folder = "MyUniversalApp\\LabWork1\\CoverImages";

并且使用的 xaml 在这里和运行时它正确显示了两个字符串属性,但不显示图像。

    <ListView.ItemTemplate>              
            <DataTemplate x:DataType="data:LibraryBookModel">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Width="56" Height="56">
                        <Image Source="{Binding CoverImage}" Stretch="UniformToFill"/>
                    </Border>
                    <StackPanel Grid.Column="1">
                        <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Margin="9.6,0"/>
                        <TextBlock Text="{Binding Author}" Style="{StaticResource SubtitleTextBlockStyle}" TextWrapping="NoWrap" Margin="9.6,0"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

图片 url 的构造方式有什么问题吗?

4

0 回答 0