2011-01-10 12 views
0

私のアプリケーションにグリッドがあります。ユーザーがダイアログアプリケーションでいくつかのファイルを選択すると、いくつかの計算が処理されます。アプリが計算をしている間、応答していないように見えます。計算中に何かの画像を表示してメインウィンドウを黒で表示する方法&?たぶんMainWindowのいくつかのdpを "IsBusy"にしてポップアップを画像にバインドするのでしょうか?wpf作業状態

あなたのアプリケーションにこのロジックを実装するにはどうすればよいですか?

DowloadバイナリとWPFToolkit.Extended.dllへのプロジェクト参照を追加します。

答えて

2

簡単な方法としては、Extended WPF Toolkitからビジーインジケータを使用することです。

次はあなたの "メインウィンドウの中で、次の名前空間を追加します。

xmlns:ext="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" 

は、次にビューでビジーインジケータを追加する(示されたとき、それは画面全体を占有するようにそれを置く)ここに私のメインウィンドウが持っています2行と私は両方の行にまたがるコントロールをしたい。コントロールのIsBusyプロパティは、ビューのデータコンテキスト内のboolプロパティにバインドされています。

<ext:BusyIndicator Grid.RowSpan="2" x:Name="busyIndicator" IsBusy="{Binding IsBusy}" /> 

長時間続く計算は、ユーザーインターフェイスをブロックしないように別のスレッドで処理する必要があります。スレッド化の場合はBackgroundWorker classを使用できます。

1

UIブロッキングを避けるために、別々のスレッドで実行時間の長いタスクを実行する必要があります。 ここではそれを達成できる一つの方法です:

は、以下のようにバックグラウンドスレッドを定義します。

//Delegate that you could pass into the worker thread 
public delegate void ProgressMonitor(string s); 

//Call this to start background work 
void StartLongRunningWork(ProgressMonitor mon) 
{ 
    using (BackgroundWorker bgw = new BackgroundWorker()) 
    { 
     bgw.DoWork    += WorkerThread; 
     bgw.RunWorkerCompleted += WorkerThreadCompleted; 
     bgw.RunWorkerAsync(mon); 
    } 
} 

void WorkerThread(object sender, DoWorkEventArgs e) 
{ 
    ProgressMonitor pm = (ProgressMonitor)e.Argument; 
    WorkerActual(pm, <any other parameters>); 
} 

void WorkerActual(ProgressMonitor pm,<any other parameters>) 
{ 
    ... 
    pm("Doing x"); 
    Do long running task 
    pm("Doing y"); 
    ... 
} 

//This function is called in case of Exception, Cancellation or successful completion 
//of the background worker. Handle each event appropriately 
void WorkerThreadCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Error != null) 
    { 
     //Long running task threw an exception 
    } 
    else 
     if (e.Cancelled) 
     { 
      //Long running task was cancelled 
     } 
     else 
     { 
      //Long running task was successfuly completed 
     } 
} 

し、以下のようにそれを呼び出す:

private void UpDateProgressLabel(string s) 
{ 
    this.Dispatcher.BeginInvoke((Action)delegate 
     { 
      NotificationLabel.Content = s; 
     }); 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    StartLongRunningWork(UpDateProgressLabel); 
} 
は、
関連する問題