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

时间:2017-12-19 10:37:39

标签: xamarin xamarin.forms task-parallel-library

我正在尝试使用Task Parallel Library中的Xamarin.Forms更新进度条列表以显示图像下载进度

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

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

<?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>

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

但是,我的主要问题是我无法在用户界面中看到进度更新,因此我不知道如何更新用户界面。

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;              

}

1 个答案:

答案 0 :(得分:0)

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

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

然后在你的all方法中使用mediaProgress设置进度。