更新ShowMessageAsync进度值来自"外部"方法

时间:2014-04-22 05:41:46

标签: c# wpf

我有一个使用MahApps.Metro UI资源的WPF C#应用。我尝试将progress dialog控件绑定到另一个类的方法调用,并在方法逐步执行时手动递增进度条的进度。

我无法弄清楚我将如何编程"我们在方法X的检查点1处 - 报告完成了25%。现在我们在方法X的检查点2 - 报告50%完成。"

MainWindow.xaml.cs中启动ShowMessageAsync进度条的方法如下所示:

     private async void installThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        var app = ((FrameworkElement)sender).DataContext as Tuple<ThirdPartyUpdate.InstalledApplicationFromRegistryScan, ThirdPartyUpdate.ManifestRequiredApplication, ThirdPartyUpdate.RequiredApplicationState>;

        Button ThirdPartyInstallButton = sender as Button;
        ThirdPartyInstallButton.IsEnabled = false;

        var controller = await this.ShowProgressAsync("Please wait...", "Preparing update installation...");
        await Task.Delay(2000);
        controller.SetCancelable(false);

        if (app != null)
        {
            ThirdPartyUpdate.thirdPartyApplicationInstallWorkflow(app);
        }

        controller.SetProgress(.25);
        controller.SetMessage("Closing dependant processes...");
        await Task.Delay(2000);
        controller.SetProgress(.50);
        controller.SetMessage("Uninstalling legacy version...");
        await Task.Delay(2000);
        controller.SetProgress(.75);
        controller.SetMessage("Installing new version...");
        await Task.Delay(2000);
        controller.SetProgress(.100);

        await controller.CloseAsync();
        await this.ShowMessageAsync("Success!", "Update installed successfully.");
    }

我目前在那里的controller.SetProgress条目仅用于说明目的,以显示我想要的结果。它们呈现以下内容:

enter image description here

我真正需要的是这种方法:

ThirdPartyUpdate.thirdPartyApplicationInstallWorkflow(app);

在报告某些代码段时报告其进度。如果有帮助,以下是该方法的内容:

 public static int thirdPartyApplicationInstallWorkflow(Tuple<ThirdPartyUpdate.InstalledApplicationFromRegistryScan, ThirdPartyUpdate.ManifestRequiredApplication, ThirdPartyUpdate.RequiredApplicationState> thirdPartyApp)
    {
        // 1. Close listed process
        // 2. Execute explicit uninstall
        // 3. Execute WMI uninstall
        // 4. Execute install/command          

        writeEvent(EventLogEntryType.Information, "Beginning execution of: " + thirdPartyApp.Item2.Name + " job.", "Third Party Update");

        //Close processes prior to upgrade if needed
        closeProcesses(thirdPartyApp);

        //Execute explicit uninstall if needed
        uninstallExplicit(thirdPartyApp);

        //Execute WMI uninstall if needed
        uninstallWMI(thirdPartyApp);

        //Execute install
        if (!string.IsNullOrEmpty(thirdPartyApp.Item2.InstallString))
        {
            //String cleanup on comma placement so that a few different input styles are valid
            string cleanedstrInstall = thirdPartyApp.Item2.InstallString.Replace(", ", ",").Replace(" ,", ",").Replace(" , ", ",");
            List<string> strInstall = cleanedstrInstall.Split(',').ToList<string>();
            int installExitCode = 0;
            DateTime timeLaunched;
            DateTime timeCompleted;

            foreach (var install in strInstall)
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c " + thirdPartyApp.Item2.InstallString);
                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardError = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                writeEvent(EventLogEntryType.Information, "Attempting to launch upgrade: " + thirdPartyApp.Item2.InstallString, "Third Party Update");
                timeLaunched = DateTime.UtcNow;
                proc.Start();
                string stderror = proc.StandardError.ReadToEnd();
                proc.WaitForExit();
                timeCompleted = DateTime.UtcNow;
                if (proc.ExitCode == 0 || proc.ExitCode == 3010)
                {
                    writeEvent(EventLogEntryType.Information, "Successfully completed upgrade from: " + thirdPartyApp.Item2.InstallString, "Third Party Update");
                }
            }       
        }
        return 0;
    }

1 个答案:

答案 0 :(得分:3)

只需委托控制器上的SetProgress功能。

var controller = await this.ShowProgressAsync("Please wait...", "Progress message");

在某处定义以下委托:

public delegate void UpdateProgressFunc(double value);

更改方法thirdPartyApplicationInstallWorkflow以将此委托添加为方法参数:

public static void thirdPartyApplicationInstallWorkflow(App app, UpdateProgressFunc UpdateProgress);

然后从这个函数中,在不同的阶段,只需调用:

UpdateProgress(PERCENT);