1

WPFアプリがあります。ボタンをクリックすると、アプリは4〜10秒かかる計算に入ります。その操作中に、背景の不透明度を更新してプログレスバーを表示したいと思います。

そのために、私は次のコードを使用します。

this.Cursor = System.Windows.Input.Cursors.Wait;

// grey-out the main window
SolidColorBrush brush1 = new SolidColorBrush(Colors.Black);
brush1.Opacity = 0.65;
b1 = LogicalTreeHelper.FindLogicalNode(this, "border1") as Border;
b1.Opacity = 0.7;
b1.Background = brush1;

// long running computation happens here .... 
// show a modal dialog to confirm results here
// restore background and opacity here. 

コードを実行すると、モーダルダイアログが表示されるまで背景と不透明度が変わりません。計算を開始する前に、これらの視覚的な変更を今すぐ発生させるにはどうすればよいですか?Windowsフォームでは、各コントロールにUpdate()メソッドがあり、これは必要に応じて実行されました。WPFアナログとは何ですか?

4

2 に答える 2

1

バックグラウンド スレッドで長時間実行される計算を行う場合はどうなるでしょうか。完了したら、結果を UI スレッドにディスパッチします...

正直なところ、あなたの問題を解決できるものは他にないと思います。ネストされたポンピングがうまくいくかもしれませんが、私はそれを本当に疑っています.

このリファレンスが役立つ場合に備えて: Dispatcher を使用してよりレスポンシブなアプリを構築する

于 2010-02-26T15:14:42.453 に答える
0

次に示すように、DoEvents()コードを使用します:http:
//blogs.microsoft.co.il/blogs/tamir/archive/2007/08/21/How-to-DoEvents-in-WPF_3F00_.aspx

私の実際のコード:

private void GreyOverlay()
{
    // make the overlay window visible - the effect is to grey out the display
    if (_greyOverlay == null)
        _greyOverlay = LogicalTreeHelper.FindLogicalNode(this, "overlay") as System.Windows.Shapes.Rectangle;
    if (_greyOverlay != null)
    {
        _greyOverlay.Visibility = Visibility.Visible;
        DoEvents();
    }
}

private void DoEvents()
{
    // Allow UI to Update...
    DispatcherFrame f = new DispatcherFrame();
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                                             new Action<object>((arg)=> {
                                                     DispatcherFrame fr = arg as DispatcherFrame;
                                                     fr.Continue= false;
                                                 }), f);
    Dispatcher.PushFrame(f);
}
于 2010-02-26T18:10:50.630 に答える