C# 如何将本地下载的图像设置为BitmapImage?

C# 如何将本地下载的图像设置为BitmapImage?,c#,windows-phone,C#,Windows Phone,我已经下载了带有图像控件的图像、类和列表视图 这是我绑定到listview的类: class MagazineDownload { public string Title { get; set; } public string Date { get; set; } public BitmapImage Cover { get; set; } public string Pdf { get; set; } p

我已经下载了带有图像控件的图像、类和列表视图 这是我绑定到listview的类:

class MagazineDownload
    {
        public string Title { get; set; }
        public string Date { get; set; }
        public BitmapImage Cover { get; set; }
        public string Pdf { get; set; }

        public MagazineDownload(string title, string image, string date, string pdf)
        {
            Title = title;
            Cover = new BitmapImage();
            addImage(image);
            Date = date;
            Pdf = pdf;
        }


        private async void addImage(string image)
        {
            StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(image);
            IAsyncOperation<IRandomAccessStream> operation = storageFile.OpenAsync(FileAccessMode.Read);
            IRandomAccessStream stream = await operation;
            Cover.SetSource(stream);      
        }
    }
这是图像绑定的代码:

<ListView
                    Margin="19,-23,-19.167,23.333"
                    AutomationProperties.AutomationId="PivotListViewSection"
                    SelectionMode="None"
                    IsItemClickEnabled="False"
                    ItemClick="downList_ItemClick"
                    ItemsSource="{Binding}"
                    x:Uid="downList"
                    x:Name="downList"
                    >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="imageDownCover" Height="100" Width="100" Stretch="Fill" Source="{Binding Cover}"/>
除图像外,所有标题、日期、pdf按钮标记都有效


如何修复它?

避免从构造函数调用任何异步方法。它将被设置,但需要通过INotifyPropertyChanged通知UI

由于映像是本地文件,因此可以使用本地文件夹uri,只需在构造函数中设置uri即可

public MagazineDownload(string title, string image, string date, string pdf)
{
    //don't forget the triple forward slashes
    var uri = string.Format("ms-appdata:///local/{0}", image);

    Title = title;
    Cover = new BitmapImage(new Uri(uri));
    Date = date;
    Pdf = pdf;
}
我建议设置INotifyPropertyChanged,因为您的所有属性都是public get和set,如果这些值中的任何一个发生更改,您都希望UI更新。如果它们不应该更改,请使它们具有一个私有的只读支持变量,这样就没有任何东西可以更改它们。

正如您在内部等待的那样,稍后将在构造函数完成后设置封面。您需要添加NofityPropertyChanged以通知UI图像已加载