将图像绑定到Windows Universal应用程序中的ListView From Local文件夹

时间:2015-12-14 10:26:03

标签: c# uwp windows-10-universal

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

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

  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>

图像网址的构建方式有什么问题吗?

0 个答案:

没有答案