2010-02-26 4 views
1

WPF 앱이 있는데, 버튼을 클릭하면 앱이 4-10 초 정도 걸릴 수 있습니다. 배경의 불투명도를 업데이트하고 해당 작업 중에 진행률 표시 줄을 표시하고 싶습니다. 나는이 코드를 실행하면 모달 대화 상자가 표시 될 때까지WPF : 불투명도/배경 변경을 즉시 적용하는 방법? (WPF analog to WinForms Control.Update() 메서드?)

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 Forms에는 각 컨트롤에 필요한대로 Update() 메서드가있었습니다. WPF 아날로그 란 무엇입니까?

답변

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); 
} 
+0

그래서 중첩 된 펌핑을 사용하고 있습니다. 흥미 롭습니다. 나는 그것이 효과가있을 것이라고 기대하지 않았다. – Anvaka

1

백그라운드 스레드에서 계산을 오래 실행하면 어떨까요? 일단 그들이 완료되면 UI 스레드로 결과를 파견합니다 ...

솔직히, 나는 그곳에는 문제가 해결되지 않는다고 의심합니다. 어쩌면 nested pumping 트릭을 할 것입니다,하지만 난 정말 그것을 의심.

그냥 경우이 참조 도움이됩니다 : Build More Responsive Apps With The Dispatcher

+0

꽤 게시 이후 몇 가지. BackgroundWorker에서 계산을하는 것도 도움이되지 못했습니다. 때로는이 스레드에 대한 응답 중 하나에서 설명한대로 DispatcherFrame을 사용하여 원하는 것을 얻을 수 있습니다 (http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6fce9b7b-4a13-4c8d-8c3e- 562667851baa /)하지만 내 결과가 일관성이 없습니다. 때로는 효과가 있고 그렇지 않은 경우도 있습니다. 여전히 쳐다보다. – Cheeso

관련 문제