ShowDialogue 未生成 DialogueResult.OK

ShowDialogue not generating DialogueResult.OK

我正在编写一个程序,应该从 GitHub 下载一些东西。它有一个 link 到 GitHub 上的原始文件。我正在使用 DownloadDataAsync 下载它,我有一个进度条来跟踪下载进度。它总是达到 100%,但之后什么都不做。

我一直在关注 BetterCoder 的 C# 更新程序教程(开头是 here,最相关的部分是本系列的第 9 部分)。

这是它停止正常工作的部分:

private void DownloadUpdate(SaveyourUpdateXML update)
{
    SharpUpdateDownloadForm form = new SharpUpdateDownloadForm(update.Uri, update.MD5, this.applicationInfo.ApplicationIcon);
    Debug.WriteLine("form created");
    DialogResult result = form.ShowDialog(this.applicationInfo.Context);
    Debug.WriteLine("got result");

    if (result == DialogResult.OK)
    {
        String currentPath = this.applicationInfo.ApplicationAssembly.Location;
        String newPath = Path.GetDirectoryName(currentPath) + "\" + update.FileName;

        UpdateApplication(form.TempFilePath, currentPath, newPath, update.LaunchArgs);

        Application.Exit();
    }
}

它永远不会到达 "got result" 部分,除非我取消它。还有,this.applicationInfo.Contextreturns一个表格。但是,它确实说 "form created".

我认为 ShowDialog 的使用方式或其他方面有问题,但我不太确定是什么。

编辑: 这是创建 SharpUpdateDownloadForm 时发生的情况。

internal SharpUpdateDownloadForm(Uri location, String md5, Icon programIcon)
{
    InitializeComponent();
    if (programIcon != null)
    {
        this.Icon = programIcon;
    }

    tempFile = Path.GetTempFileName();
    this.md5 = md5;
    webClient = new WebClient();

    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);

    bgWorker = new BackgroundWorker();
    bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);

    try
    {
        webClient.DownloadDataAsync(location, this.tempFile);
    }

    catch
    {
        this.DialogResult = DialogResult.No;
        this.Close();
    }
}

这是下载完成后应该发生的情况:

private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        this.DialogResult = DialogResult.No;
        this.Close();
    }
    else if (e.Cancelled)
    {
        this.DialogResult = DialogResult.Abort;
        this.Close();
    }
    else
    {
        lblProgress.Text = "Verifying Download...";
        progressBar.Style = ProgressBarStyle.Marquee;

        bgWorker.RunWorkerAsync(new string[] {this.tempFile, this.md5});
    }
}

这是bgWorker_RunWorkerCompleted

private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.DialogResult = (DialogResult)e.Result;
    this.Close();
}

和bgWorker_DoWork

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
        String file = ((string[])e.Argument)[0];
        String updateMD5 = ((string[])e.Argument)[1];

        if (Hasher.HashFile(file, HashType.MD5) != updateMD5)
            e.Result = DialogResult.No;
        else
            e.Result = DialogResult.OK;
    }

webClient.DownloadDataAsync 完成时,它会触发 DownloadDataCompleted 事件,而不是 DownloadFileCompleted - 您注册的事件。

修复方法是,如果您使用 webClient.DownloadDataAsync,请注册 DownloadDataCompleted 事件;请注意 webClient_DownloadDataCompleted 的第二个参数是不同的。

webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);

...

private void webClient_DownloadDataCompleted(Object sender, DownloadDataCompletedEventArgs e)
{ 
    ...
}