【问题标题】:Update ProgressBar with WebClient使用 WebClient 更新 ProgressBar
【发布时间】:2015-08-24 08:47:10
【问题描述】:

我正在制作一个 WPF 应用程序,我使用 WebClient 从 Web 服务器下载文件。我的下载工作正常。现在我想将我的 e.ProgressPercentage 绑定到 ProgressBar。我不知道如何做到这一点。每当我想下载一个文件时,我都会调用 StartDownload 来调用 DownloadProtocol

class DownloadGameFile
{
    public string path { get; set; }

    private DownloadTorrentFile DLTorrent;

    //List of file that already exist
    private List<string> ExistFile = new List<string>();
    DirectoryInfo fileInfo;

    private string savePath = @"C:\Program Files (x86)\something\Client\package\downloads\";


    private bool isDelete = false;
    public DownloadGameFile()
    {
        DLTorrent = new DownloadTorrentFile();

    }
    public async Task StartDownload(int torrentId)
    {
        try
        {
            DLTorrent.DecodeTorrent(torrentId);

            downloadGameCover(torrentId);

            //Creates a folder to the game
            var newdic = Directory.CreateDirectory(savePath + torrentId);

            fileInfo = new DirectoryInfo(savePath + torrentId);
            //File info from a Directory
            FileInfo[] files = fileInfo.GetFiles();

            foreach (FileInfo i in files)
            {
                Console.WriteLine("Files exit " + i);
                if (DLTorrent.GameInfomation[i.Name] != i.Length)
                {
                    i.Delete();
                    isDelete = true;
                }
                else
                {
                    Console.WriteLine("add files ");
                    ExistFile.Add(i.Name);
                }

            }

            // Get name form api
            string gameName = MainWindow.getGameName(torrentId);
            List<Game> games = MyGames.GetList();

            games.Add(new Game(torrentId, gameName, "", false, false, "http://cdn.something.com/rental/" + torrentId + ".torrent"));
            MyGames.SaveMyGames(games);

            //Make a list which file not downloaded yet
            var res = DLTorrent.GameInfomation.Keys.Except(ExistFile);

            var nFiles = files.Length;
            if (nFiles == 0 || !isDelete)
            {
                nFiles++;
                foreach (var x in res)
                {
                    Console.WriteLine("\r {0} out of {1}", nFiles, DLTorrent.GameInfomation.Keys.Count());
                    await DownloadProtocol("http://cdn.something.com/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);
                    nFiles++;
                }
            }
            else
            {
                foreach (var x in res)
                {

                    Console.WriteLine("\r {0} out of {1}", nFiles, DLTorrent.GameInfomation.Keys.Count());
                    await DownloadProtocol("http://cdn.something.com/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);

                    nFiles++;
                }
            }
        }
        catch
        {

        }

    }


    public async Task DownloadProtocol(string address, string location)
    {

        Uri Uri = new Uri(address);
        using (WebClient client = new WebClient())
        {
            client.DownloadProgressChanged += (o, e) =>
            {
                Console.Write("\r{0} Bytes - {1}%", e.BytesReceived, e.ProgressPercentage);
            };

            client.DownloadFileCompleted += (o, e) =>
            {
                if (e.Cancelled == true)
                {
                    Console.WriteLine("Download has been canceled.");
                }
                else
                {

                    Console.WriteLine("Download completed!");
                }

            };

            await client.DownloadFileTaskAsync(Uri, location);
        }

    }

<ProgressBar x:Name="pb" HorizontalAlignment="Left" Width="150" Height="25" Margin="0,0,0,0" Value="{Binding Path=??}"</ProgressBar>

【问题讨论】:

    标签: c# wpf data-binding progress-bar


    【解决方案1】:

    MainWindow中创建一个公共方法

    public void UpdateBar(int value)
       {
           pb.Value=value;
       }
    

    然后从你的MainWindow调用它

     client.DownloadProgressChanged += (o, e) =>
            {
                UpdateBar(e.Percentage);
            };
    

    由于它是公开的,因此您可以在课堂上使用它。 这将根据 webclient 更新下载进度。

    【讨论】:

    • 我无法从另一个类@Slashy 访问我的 ProgressBar
    • @LocDaiLe 但为什么呢?您上面显示的所有代码都在DownloadGameFile.. 下?
    • 对不起,如果我还没有说清楚。 DownloadGameFIle 是一个类,进度条在 MainWindow.xaml.cs @Slashy
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多