使用任务并行库更新进度条

updating progress bar using Task parallel library

我正在尝试使用 Xamarin.Forms

中的 Task Parallel Library 更新进度条列表以显示图像下载进度

现在我已经写了一段代码来模拟使用延迟的下载过程。

这是我的 Xaml 文件,其中有一个名为 MediaListListView,每个项目都有一个标题和进度条。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ImageTask.View.ImageTaskView">
    <ContentPage.Content>
        <ListView ItemsSource="{Binding MediaList}" CachingStrategy = "RecycleElement" VerticalOptions="FillAndExpand" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical">
                            <Label Text = "{Binding mediaName}" FontSize="22" />
                            <ProgressBar Progress="{Binding mediaProgress}"></ProgressBar>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

这是我的视图模型,我在其中创建了一个操作块,它采用整个媒体列表 object 并尝试更新进度条。

但是,我的主要问题是我无法在我的 UI 中看到更新的进度,所以我不知道该如何更新我的 UI.

public class ImageTaskViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private IList<MediaInfo> _mediaList;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    public IList<MediaInfo> MediaList
    {

        get { return _mediaList; }
        set
        {

            _mediaList = value;
            OnPropertyChanged("MediaList");

        }
    }
    public ImageTaskViewModel()
    {

       Action<IList<MediaInfo>> progressActionBlock = mediaInfoList =>
       {
           // infinite loop to simulate download
           while (true)
           {
               IEnumerator<MediaInfo> dataList = mediaInfoList.GetEnumerator();

               Task.Delay(2000).Wait();           

               while (dataList.MoveNext())
               {

                   MediaInfo mediaInfo = dataList.Current;

                   Debug.WriteLine("media name " + mediaInfo.mediaName + " progress " + mediaInfo.mediaProgress);


                   if (mediaInfo.mediaProgress == 1)
                   {
                       Debug.WriteLine("media name " + mediaInfo.mediaName + " Done ");
                       break;

                   }
                   else
                   {
                       mediaInfo.mediaProgress = mediaInfo.mediaProgress + 0.1;
                   }                       
               }                   
           }

       };

        var opts = new ExecutionDataflowBlockOptions()
        {
            MaxDegreeOfParallelism = 2

        };
        var progressAction = new ActionBlock<IList<MediaInfo>>(progressActionBlock, opts);           

        MediaList = new List<MediaInfo>();

        for (int i = 1; i < 6; i++)
        {
            MediaInfo mediaInfo = new MediaInfo();
            mediaInfo.mediaName = i.ToString();
            MediaList.Add(mediaInfo);

        }
        // Exectue Action block
        progressAction.Post(MediaList);
    }
}

MediaInfo 型号:

public class MediaInfo
{      
    public string mediaId { get; set; }
    public string mediaName { get; set; }
    public string mediaPath { get; set; }     
    public byte[] mediaStream { get; set; }
    public double mediaProgress { get; set; } = 0.1;              

}

在您的视图模型中,您没有 mediaProgress。你应该添加

    Double _mediaProgress;
    public Double mediaProgress{
        get{
            return _mediaProgress;
        } set{
            _mediaProgress = value;
            OnPropertyChanged("Email");
        }
    }

然后在您的所有方法中使用 mediaProgress 设置进度。