我原本没有计划实现BusyIndicator,但我意识到我的Powershell脚本需要一些时间来执行,这可能会导致与用户混淆。似乎没有任何教程显示如何在C#中使用带有Powershell脚本的BusyIndicator。 This是一个很棒的教程,因为它是由WPF扩展工具包的作者编写的,但它不是我需要的。
这就是我所拥有的。为简洁起见,代码被截断:
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
我对此很新。任何帮助将不胜感激。
答案 0 :(得分:1)
您可以在serapate线程上运行代码(使用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);
}