gpt4 book ai didi

c# - 从 "External"方法更新 ShowMessageAsync 进度值

转载 作者:行者123 更新时间:2023-11-30 16:12:17 31 4
gpt4 key购买 nike

我有一个使用 MahApps.Metro UI 的 WPF C# 应用程序资源。我正在尝试绑定(bind) 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;
}

最佳答案

只需委托(delegate)给 Controller 上的 SetProgress 函数即可。

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

在某处定义以下委托(delegate):

public delegate void UpdateProgressFunc(double value);

更改您的方法 thirdPartyApplicationInstallWorkflow 以将此委托(delegate)添加为方法参数:

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

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

UpdateProgress(PERCENT);

关于c# - 从 "External"方法更新 ShowMessageAsync 进度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23211259/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com