我目前正在製作一個小型迷你遊戲。遊戲有能力打開一些選項,在選項中你可以點擊「啓用音樂」。由於應用程序包括音頻文件太大,我希望用戶選擇他是否需要音樂,並且必須下載它。使用AsyncTask下載文件並在進度條中顯示進度
我使用的是異步任務此選項,因爲選項應該彈出式同時下載,這是到目前爲止我的代碼:
public void CheckForFiles()
{
// searches the current directory and sub directory
int fCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length;
//If there are NOT all of the Audiofiles, delete all of them and re-download!
if (fCount != 2)
{
//Just to be sure, delete all previous files!
Array.ForEach(Directory.GetFiles(@"C:\Users\Public\Documents\Ultimate Tic-Tac-Toe\Audios\"), File.Delete);
//Change the Button from "Apply" to "Download"
apply_options.Text = "Download";
//Set the Warning Text
warning_text.Text = "Warning! Downloading the needed Audiofiles. DO NOT interrupt the Proccess!";
//SHow the Download-Progress Bar
download_progress.Visible = true;
//Download all
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += (s, y) =>
{
download_progress.Value = y.ProgressPercentage;
};
webClient.DownloadFileCompleted += (s, y) =>
{
download_progress.Visible = false;
warning_text.Text = "Complete!";
CheckForFiles();
};
webClient.DownloadFileAsync(new Uri(remoteUri_dark), fileName_dark);
//Text = "Downloading File one of" + WAITLIST;
}
此下載一個文件,但我需要兩個。 所以我試圖等待我的進度欄填寫,下載下一個,如果我有2個文件,完成!但該代碼跳轉到「DownloadFileCompleted」,所以這是行不通的。我現在正坐在這裏約2個小時左右,擺弄。
如何獲得異步任務創建兩個文件的「Que」,下載它們然後跳轉到「DownloadFileCompleted」並仍顯示進度? 謝謝!
進度可以包括公式中的文件總數(如果你下載10個文件和文件5下載40%,那麼它是54%顯示),並在下載結束文件'X'(在'DownloadFileCompleted'?)中,您可以開始加載另一個文件,直到下載完所有文件。 – Sinatr