简体   繁体   English

创建带有更新进度条的ListView以下载Windows 8 C#XAML

[英]Creating a ListView with updating Progress bar for downloads Windows 8 C# XAML

I am trying to create a ListView that holds a collection of downloads, each with their own progress bar. 我正在尝试创建一个ListView来保存下载的集合,每个下载都有自己的进度栏。

The current method I am using is to bind a class that holds the information of currently downloading items, including the current completion percentage of the download: 我正在使用的当前方法是绑定一个类,该类包含当前正在下载的项目的信息,包括当前下载的完成百分比:

The item class: 物品类别:

public class DownloadItem
{
    public double downloadPercent { get; set; }
    public string episodeTitle { get; set; }
    public string podcastFeedTitle { get; set; }
    public DownloadOperation operation { get; set; }

    public double percent
    {
        get;
        set;
    }
}

The observableCollection that holds them 持有它们的observableCollection

public ObservableCollection<DownloadItem> downloadInformationList = new ObservableCollection<DownloadItem>();

The method that's called when progress for the item is changed: 更改项目进度时调用的方法:

private void DownloadProgress(DownloadOperation download)
{
    double percent = 100;

    if (download.Progress.BytesReceived > 0)
    {
        percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;
        Debug.WriteLine(percent);
    }

    foreach (DownloadItem item in downloadInformationList)
    {
        if (item.operation == download)
        {
            item.percent = percent;
        }
    }
}

And the XAML code for the itemTemplate for the ListView: 以及ListView的itemTemplate的XAML代码:

<DataTemplate>
    <StackPanel>
        <TextBlock Text="{Binding episodeTitle, Mode=TwoWay}" />
        <ProgressBar IsIndeterminate="False"
                     Value="{Binding percent, Mode=TwoWay}"
                     Maximum="100"
                     Width="200" />
    </StackPanel>
</DataTemplate>

The ProgressBar works and updates, however it ONLY updates upon returning to the page, not in real-time. ProgressBar可以工作和更新,但是仅在返回页面时才更新,而不是实时更新。 What am I doing wrong? 我究竟做错了什么? Any help would be much appreciated! 任何帮助将非常感激!

Your DownloadItem class needs to implement INotifyPropertyChanged to reflect the real time changes to the Percent property 您的DownloadItem类需要实现INotifyPropertyChanged,以反映对Percent属性的实时更改

public class DownloadItem : INotifyPropertyChanged
{
    public double downloadPercent { get; set; }
    public string episodeTitle { get; set; }
    public string podcastFeedTitle { get; set; }
    public DownloadOperation operation { get; set; }

    private double percent;
    public double Percent
    {
        get { return percent; }
        set
        {
            if (percent == value)
                return;

            percent = value;
            OnPropertyChanged("Percent");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Implementing INotifyPropertyChanged 实现INotifyPropertyChanged

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

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