显示进度视图,直到async/await任务完成

本文关键字:await 任务 async 直到 视图 显示 | 更新日期: 2023-09-27 17:53:07

我有两个任务,从本地存储内的web地址下载mp3并返回timeSpan。

 public async Task<TimeSpan> filedownload_Ignition_Outside()
            {
                Uri sourceIgnition_Outside = new Uri(TemporaryAddress.download_playCarSound+"Ignition_Outside");
//download and store file
                return duration_Ignition_Outside;
            }



public async Task<TimeSpan> filedownload_Idle_Outside()
    {
        Uri sourceIdle_Outside = new Uri(TemporaryAddress.download_playCarSound +"Idle_Outside");
        StorageFile destinationFileIdle_Outside;
       //download and store file
        return duration_Idle_Outside;
    }

现在我需要在UI中显示一个不确定的进度条,而下载开始直到它结束,但不知道如何找到完成的任务?

在我的NavigatedTo函数上,我已将其设置为异步并且正在调用等待downloadFile ();

现在在我的downloadFile()

 public async Task<TimeSpan> downloadFiles()
            {
                //ProgressShow

              var temp= await filedownload_Ignition_Outside();

                //if (filedownload_Ignition_Outside().IsCompleted)
                //{
                //    //progressStop
                //}
return temp;
    }

但是它没有执行停止语句,因为它异步等待,我怎么能得到两个任务完成的事件?
我可以在我的downloadFiles()方法中调用两个任务并且仍然能够获得两个任务完成的事件吗?

显示进度视图,直到async/await任务完成

在您的OnNavigatedTo事件中尝试类似于下面给出的代码。

          ShowProgress=true;
           downloadFiles().ContinueWith((sender) =>
            {
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                   ShowProgress=false;
                );
            });

您需要在UI线程中运行ShowProgress=False;