我最初没有计划实现BusyIndicator,但我意识到我的Powershell脚本需要花费一些时间来执行,这可能会引起与用户的混淆。似乎没有任何教程显示如何在C#中将BusyIndicator与Powershell脚本一起使用。这是一个很棒的教程,因为它是由WPF Extended Toolkit的作者编写的,但是它并不是我所需要的。
这就是我所拥有的。为了简洁起见,代码被截断了:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void imageBtn_Click(object sender, RoutedEventArgs e)
{
Button imgBtn = sender as Button;
string appToCheck = GetSubString(imgBtn.Name);
switch(appToCheck)
{
case "weather":
InvokePowerShell(appToCheck);
break;
case "news":
//DO THE SAME FOR ALL CASES
break;
//17 more cases follow
}
}
private string GetSubString(string buttonName)
{
int index = buttonName.IndexOf('B');
return buttonName.Substring(0, index);
}
private void InvokePowerShell(string str)
{
str = char.ToUpper(str[0]) + str.Substring(1);
PowerShell ps = PowerShell.Create();
ps.AddScript("Get-AppxPackage | Select Name | Where-Object ($_.Name -like '*" + str + "*'}");
_busyIndicator.IsBusy = true;
ps.BeginInvoke<PSObject>(null, new PSInvocationSettings(), ar =>
{
try
{
var psOutput = ps.EndInvoke(ar);
this.Dispatcher.Invoke(() => _busyIndicator.IsBusy = false);
foreach (PSObject item in psOutput)
{
if (item.Equals(String.Empty))
{
MessageBox.Show(str + " is not installed so cannot be removed.");
}
else
{
if (MessageBox.Show("This cannot be undone.\nContinue?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
{
//DO NOTHING
}
else
{
//TODO Remove the app
MessageBox.Show(str + " successfully removed.");
}
}
}
}
finally
{
//dispose of it
ps.Dispose();
}
}, null);
}
}
}
我已经在XAML中设置了BusyIndicator:
<toolkit:BusyIndicator x:Name="_busyIndicator" IsBusy="False" BusyContent="One moment....">
<!-- Insert the rest of my markup here -->
</toolkit:BusyIndicator>
我还将有一个更长的方法来删除应用程序中列出的所有内容,因此,我一定会想要该指示器。我尝试遵循上面链接中给出的教程,但是我的foreach循环超出范围时遇到了问题。
我试图使用异步BeginInvoke()方法无济于事。繁忙指示器一直持续运行,如下所示:
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject>(outputCollection);
while (result.IsCompleted == false)
{
_busyIndicator.IsBusy = true;
}
//Then my foreach loop shown above with the if statements and MessageBox notifications
我对此很陌生。任何帮助将不胜感激。
您可以在锯齿状线程(具有Task.Run
或类似构造)上运行代码,或使用BeginInvoke
如下代码:
private void InvokePowerShell(string str) {
str = char.ToUpper(str[0]) + str.Substring(1);
// remove using, or move to a field
PowerShell ps = PowerShell.Create();
ps.AddScript("Get-AppxPackage | Select Name | Where-Object {$_.Name -like '*" + str + "*'}");
//This is where the app pauses slightly and I need a busyindicator
_busyIndicator.IsBusy = true;
ps.BeginInvoke<PSObject>(null, new PSInvocationSettings(), ar => {
try {
var psOutput = ps.EndInvoke(ar);
// note, you are not on UI thread here
this.Dispatcher.Invoke(() => _busyIndicator.IsBusy = false);
// the rest of your code here
}
finally {
// if did not move to a field - dispose here
ps.Dispose();
}
}, null);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句