C# 正在从“更新ShowMessageAsync进度值”;“外部”;方法
我有一个使用资源的WPF C#应用程序。我试图将控件绑定到另一个类的方法调用,并在方法逐步完成时手动增加进度条的进度 我很难理解如何编程“我们在方法X的检查点1-报告完成25%。现在我们在方法X的检查点2-报告完成50%” MainWindow.xaml.cs中启动ShowMessageAsync进度条的方法如下所示:C# 正在从“更新ShowMessageAsync进度值”;“外部”;方法,c#,wpf,C#,Wpf,我有一个使用资源的WPF C#应用程序。我试图将控件绑定到另一个类的方法调用,并在方法逐步完成时手动增加进度条的进度 我很难理解如何编程“我们在方法X的检查点1-报告完成25%。现在我们在方法X的检查点2-报告完成50%” MainWindow.xaml.cs中启动ShowMessageAsync进度条的方法如下所示: private async void installThirdPartyUpdatesButton_Click(object sender, RoutedEventAr
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.");
}
当它遇到某些代码段时报告其进度。以下是该方法的内容,以防有帮助:
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;
}
public static int thirdpartyaapplication安装工作流(元组thirdPartyApp)
{
//1.封闭式流程
//2.执行显式卸载
//3.执行WMI卸载
//4.执行安装/安装命令
writeEvent(EventLogEntryType.Information,“开始执行:”+thirdPartyApp.Item2.Name+“作业”,“第三方更新”);
//如果需要,请在升级之前关闭流程
封闭过程(第三方PP);
//如果需要,执行显式卸载
第三方(第三方PP);
//如果需要,执行WMI卸载
第三方(第三方);
//执行安装
如果(!string.IsNullOrEmpty(thirdPartyApp.Item2.InstallString))
{
//逗号位置上的字符串清理,以便几个不同的输入样式有效
string cleanedstrInstall=thirdPartyApp.Item2.InstallString.Replace(“,”,“,”).Replace(“,”,“,”).Replace(“,”,”).Replace(“,”,”);
List strInstall=cleanedstrInstall.Split(',').ToList();
int installExitCode=0;
时间启动的日期时间;
完成日期时间;
foreach(在strInstall中安装var)
{
ProcessStartInfo procStartInfo=新的ProcessStartInfo(“cmd.exe”,“/c”+thirdPartyApp.Item2.InstallString);
//重定向标准输出需要以下命令。
//这意味着它将被重定向到Process.StandardOutput StreamReader。
procStartInfo.RedirectStandardOutput=true;
procStartInfo.RedirectStandardError=true;
procStartInfo.UseShellExecute=false;
//不要创建黑色窗口。
procStartInfo.CreateNoWindow=true;
//现在我们创建一个进程,分配它的ProcessStartInfo并启动它
System.Diagnostics.Process proc=新的System.Diagnostics.Process();
proc.StartInfo=procStartInfo;
writeEvent(EventLogEntryType.Information,“正在尝试启动升级:”+thirdPartyApp.Item2.InstallString,“第三方更新”);
timeLaunched=DateTime.UtcNow;
proc.Start();
字符串stderror=proc.StandardError.ReadToEnd();
进程WaitForExit();
timeCompleted=DateTime.UtcNow;
如果(proc.ExitCode==0 | | proc.ExitCode==3010)
{
writeEvent(EventLogEntryType.Information,“已成功完成从以下位置的升级:”+thirdPartyApp.Item2.InstallString,“第三方更新”);
}
}
}
返回0;
}
只需委托控制器上的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);
UpdateProgress(PERCENT);