BusyIndicatorを実装する予定はありませんでしたが、Powershellスクリプトの実行に時間がかかることがわかり、ユーザーと混乱する可能性があります。 BusyIndicatorをPowerShellスクリプトでC#で活用する方法を示すチュートリアルはありません。 Thisは、WPF Extended Toolkitの作成者によって書かれたので、素晴らしいチュートリアルですが、必要なものではありません。WPFアプリケーションでBusyIndicatorをPowershellスクリプトの実行で使用する方法
これは私が持っているものです。
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);
}
}
}
私はすでにBusyIndicatorは私のXAMLで設定しています:コードを簡潔にするために切り捨てられること
<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
私は非常にこれに慣れています。どんな助けでも大歓迎です。
。手伝ってくれてどうもありがとう。それがうまくいくなら私は喜んでそれを受け入れるでしょう。 – IRGeekSauce
さて、私は私のマシンに戻っています。そして悲しいことに、BusyIndicatorは決して止まらないのです。私はメッセージボックスのif/elseステートメントを含め、あなたの "残りのコードはここ"と言ったところで私のforeach(PSObject)ループを追加しました。インジケータはちょうど行くと行く。 – IRGeekSauce
更新後に完全なコードを投稿してください。あなたはIsBusy =偽の部分を見逃しませんでしたか? – Evk