简体   繁体   English

如何更新图片 <image> 在Windows Phone 8.1的ListView中

[英]how to update image of <image> in listview in windows phone 8.1

this is my list view 这是我的列表视图

<ListView x:Name="Diary" Margin="0,0,0,0" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="diaryTemplate" Margin="18,10,18,0" Background="White">
                            <Image Name="arrowImage" Source="{Binding img, Mode=OneWay}"   ></Image>
                            <TextBlock x:Name="DiscriptionOfImage" Height="40" Foreground="#FF070719" Text="{Binding title}" FontSize="26.667" FontWeight="Bold" Margin="15,0"/>
                            <TextBlock x:Name="DateAndTime" Text="{Binding date}" HorizontalAlignment="Left" Height="60" Foreground="#FF919192" Margin="10,25,0,0" FontSize="30" VerticalAlignment="Top"></TextBlock>
                            <Button x:Name="readMoreButton" Background="#FF121213" Content="Read More" Margin="0,0,0,0" Height="60" Click="read_Click"></Button>
                        </StackPanel>

                    </DataTemplate>
                </ListView.ItemTemplate>

            </ListView>

i have bind it with 我已经绑定了

 List<ArrowItem> items = new List<ArrowItem>();
 Diary.ItemsSource = items;
 public class ArrowItem
 {
    public string title { get; set; }
    public String date { get; set; }
    public String img_link { get; set; }
    public ImageSource img { get; set; }             
 }

I have a list containing title,date,img_link inside items Now I am trying to update the image in my list view. 我有一个在项目内包含title,date,img_link的列表。现在,我试图在列表视图中更新图像。 I am trying this code 我正在尝试此代码

  private async void getImage()
    {
        int a = items.Count();
        for (int i = 0; i< a; i++)
       { 
            var httpClient = new HttpClient();
            Stream st = await httpClient.GetStreamAsync(items[2].img_link);
            var memoryStream = new MemoryStream();
            await st.CopyToAsync(memoryStream);
            memoryStream.Position = 0;
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(memoryStream.AsRandomAccessStream());
           // items[i].img = bitmap;

        }
    }

but not getting image displayed on my UI. 但没有在我的UI上显示图像。

First of all your ArrowItem class needs to implement the INotifyPropertyChanged Interface so that when the image is updated the UI will be notified so change it to this : 首先 ,您的ArrowItem类需要实现INotifyPropertyChanged接口,以便在更新图像时会通知UI,因此将其更改为:

public class ArrowItem:INotifyPropertyChanged
{
    private string _title;
    private string _date;
    private string _imgLink;
    private ImageSource _img;

    public string Title
    {
        get { return _title; }
        set
        {
            if (value == _title) return;
            _title = value;
            OnPropertyChanged();
        }
    }

    public String Date
    {
        get { return _date; }
        set
        {
            if (value == _date) return;
            _date = value;
            OnPropertyChanged();
        }
    }

    public String ImgLink
    {
        get { return _imgLink; }
        set
        {
            if (value == _imgLink) return;
            _imgLink = value;
            OnPropertyChanged();
        }
    }

    public ImageSource Img
    {
        get { return _img; }
        set
        {
            if (Equals(value, _img)) return;
            _img = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Secondly to correctly bind your ListView to the List, create an ObservableCollection that will hold all the listView 's items (use ObservableCollection instead of List so that when an item is added of removed from the Collection the UI will be notified): 其次,要正确地将ListView绑定到List,创建一个ObservableCollection ,它将保存所有listView的项目(使用ObservableCollection而不是List以便当添加一个项目从Collection中移除时,将通知UI):

 private ObservableCollection<ArrowItem> _items  =new ObservableCollection<ArrowItem>()
    {
        new ArrowItem()
        {
            Date = "date",
            ImgLink="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSwJFp_3SZyTiHkkJpQYHsjJc99M_fJByivuuhqIdzMTo4lBgpH",
            Title = "Fruits"
        }
    };

    public ObservableCollection<ArrowItem> Itmes
    {
        get
        {
            return _items;
        }

        set
        {
            if (_items.Equals(value))
            {
                return;
            }

            _items = value;               
        }
    }

Third update you GetImages to the following : ThirdGetImages更新为以下内容:

private async Task GetImages()
    {            
        foreach (var item in Itmes)           
        {
            var httpClient = new HttpClient();
            Stream st = await httpClient.GetStreamAsync(item.ImgLink);
            var memoryStream = new MemoryStream();
            await st.CopyToAsync(memoryStream);
            memoryStream.Position = 0;
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(memoryStream.AsRandomAccessStream());
            item.Img = bitmap;                
        }
    }

and call it from, lets say the loaded even of the MainPage : 然后调用它,可以说MainPage的已加载状态:

 private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        await GetImages();
    }

Finally set the DataContext and add the loaded event handler in the Xaml 最后设置DataContext并在Xaml添加已loaded事件处理程序

DataContext="{Binding RelativeSource={RelativeSource Self}}" Loaded="MainPage_OnLoaded">

<Grid>
    <ListView x:Name="Diary" Margin="0,0,0,0" ItemsSource="{Binding Itmes}">
        <ListView.ItemTemplate>
            <DataTemplate>
                 <StackPanel x:Name="diaryTemplate" Margin="18,10,18,0" Background="White">
                    <Image Name="arrowImage" Height="100"  Source="{Binding Img, Mode=OneWay}"></Image>
                    <TextBlock x:Name="DiscriptionOfImage" Height="40" Foreground="#FF070719" Text="{Binding Title}" FontSize="26.667" FontWeight="Bold" Margin="15,0"/>
                    <TextBlock x:Name="DateAndTime" Text="{Binding Date}" HorizontalAlignment="Left" Height="60" Foreground="#FF919192" Margin="10,25,0,0" FontSize="30" VerticalAlignment="Top"></TextBlock>
                    <Button x:Name="readMoreButton" Background="#FF121213" Content="Read More" Margin="0,0,0,0" Height="60" Click="read_Click"></Button>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>
</Grid>

You should now see the image : 您现在应该看到该图像:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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