【问题标题】:Creating a ListView with updating Progress bar for downloads Windows 8 C# XAML使用更新进度条创建 ListView 以进行下载 Windows 8 C# XAML
【发布时间】:2014-01-26 12:29:02
【问题描述】:

我正在尝试创建一个 ListView,其中包含下载集合,每个都有自己的进度条。

我目前使用的方法是绑定一个类,该类保存当前下载项目的信息,包括当前下载完成百分比:

物品类别:

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;
    }
}

保存它们的 observableCollection

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

项目进度改变时调用的方法:

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;
        }
    }
}

以及 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>

ProgressBar 工作和更新,但它只在返回页面时更新,而不是实时更新。我究竟做错了什么?任何帮助将不胜感激!

【问题讨论】:

    标签: c# xaml listview windows-8 windows-store-apps


    【解决方案1】:

    您的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多