- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 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
条目仅用于说明目的,仅用于显示我想要的结果。它们呈现以下内容:
我真正需要的是这个方法:
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/
我使用 MVVM 模式创建了最简单的 WPF 项目。如何从我的 viewModel 调用 ShowMessageAsync? if (DisciplineText!="") { Disc
我正在使用 Mahapp,我正在尝试等待对话框的结果,但编译器在 ShowMessageAsync 下划线并显示我: ShowMessageAsync doesn't exist in the cur
我有一个使用 MahApps.Metro UI 的 WPF C# 应用程序资源。我正在尝试绑定(bind) progress dialog控制来自另一个类的方法调用,并在逐步执行该方法时手动增加进度条
我有一个关于 await this.ShowMessageAsync() 方法的小问题。我在谷歌上搜索了很多我的问题,但还没有成功。 我收到以下代码的错误“;预期” private void remo
我正在使用带有 MVVM 的 MahApps.metro WPF 库。我有一个 ViewModel,我需要从中显示一个对话框。 MetroWindow 有 ShowMessageAsync。但是从 V
我有这个方法 public async void setState(int state) { switch (state) {
我是一名优秀的程序员,十分优秀!