在WPF

2012-04-30 31 views
1

這裏實現暫停你有一個簡單的WPF程序:在WPF

<!-- Updater.xaml --> 
<Window x:Class="Update.Updater" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <Grid> 
     <StackPanel> 
      <Button Click="Button_Click" Height="50"></Button> 
      <Label Content="{Binding Label1Text}" Height="50"></Label> 
      <Label Content="{Binding Label2Text}" Height="50"></Label> 
     </StackPanel> 
    </Grid> 
</Window> 

// Updater.xaml.cs 
using System.Threading; 
using System.Windows; 

namespace Update 
{ 
    public partial class Updater : Window 
    { 
     public Updater() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Label1Text = "It is coming..."; 
      Thread.Sleep(3000); 
      Label2Text = "It is here!"; 
     } 

     public string Label1Text 
     { 
      get { return (string)GetValue(CategoryProperty); } 
      set { SetValue(CategoryProperty, value); } 
     } 

     static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Label1Text", typeof(string), typeof(Updater)); 

     public string Label2Text 
     { 
      get { return (string)GetValue(Label2TextProperty); } 
      set { SetValue(Label2TextProperty, value); } 
     } 

     static readonly DependencyProperty Label2TextProperty = DependencyProperty.Register("Label2Text", typeof(string), typeof(Updater)); 
    } 
} 

的目的是,當你點擊該按鈕,第一個標籤顯示It is coming...。然後程序休眠3秒鐘,最後第二個標籤顯示It is here!。但是,以下簡單的實現不起作用。如果運行它並單擊該按鈕,則會發生以下情況:程序將休眠3秒鐘,然後同時顯示兩個標籤文本。你知道如何糾正程序,使其符合預期嗎?

回答

10

Button_Click由UI線程調用,你應該不是做任何需要超過幾個毫秒的任何事情,更不用說睡3秒。在此期間不處理消息,您的界面無響應,並且您的應用程序被系統視爲「掛起」。

所以,那個長處理任務應該由另一個線程來處理。

類似的東西(未經測試):

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Label1Text = "It is coming..."; 

     var backgroundWorker = new BackgroundWorker(); 

     backgroundWorker.DoWork += (s,e) => { 
      Thread.Sleep(3000); 
     } 

     backgroundWorker.RunWorkerCompleted += (s,e) => { 
      Label2Text = "It is here!"; 
     } 

     backgroundWorker.RunWorkerAsync(); 
    } 

鏈接:

BackgroundWorker

Build More Responsive Apps With The Dispatcher

0

您可以使用情節提要和關鍵幀動畫來做到這一點。

http://msdn.microsoft.com/en-us/library/ms742868.aspx

有一個在這裏一個很好的例子 -

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.stringanimationusingkeyframes.aspx

<Button Name="myAnimatedButton" Margin="200" 
    FontSize="16pt" FontFamily="Verdana">Some Text 
    <Button.Triggers> 
    <EventTrigger RoutedEvent="Button.Click"> 
     <BeginStoryboard> 
     <Storyboard> 
      <StringAnimationUsingKeyFrames 
      Storyboard.TargetName="myAnimatedButton" Storyboard.TargetProperty="(Button.Content)" 
      Duration="0:0:8" FillBehavior="HoldEnd"> 

      <!-- All the key frames below are DiscreteStringKeyFrames. Discrete key frames create 
      sudden "jumps" between values (no interpolation). Only discrete key frames can be used 
      for String key frame animations. --> 
      <DiscreteStringKeyFrame Value="" KeyTime="0:0:0" /> 
      <DiscreteStringKeyFrame Value="A" KeyTime="0:0:1" /> 
      <DiscreteStringKeyFrame Value="An" KeyTime="0:0:1.5" /> 
      <DiscreteStringKeyFrame Value="Ani" KeyTime="0:0:2" /> 
      <DiscreteStringKeyFrame Value="Anim" KeyTime="0:0:2.5" /> 
      <DiscreteStringKeyFrame Value="Anima" KeyTime="0:0:3" /> 
      <DiscreteStringKeyFrame Value="Animat" KeyTime="0:0:3.5" /> 
      <DiscreteStringKeyFrame Value="Animate" KeyTime="0:0:4" /> 
      <DiscreteStringKeyFrame Value="Animated" KeyTime="0:0:4.5" /> 
      <DiscreteStringKeyFrame Value="Animated " KeyTime="0:0:5" /> 
      <DiscreteStringKeyFrame Value="Animated T" KeyTime="0:0:5.5" /> 
      <DiscreteStringKeyFrame Value="Animated Te" KeyTime="0:0:6" /> 
      <DiscreteStringKeyFrame Value="Animated Tex" KeyTime="0:0:6.5" /> 
      <DiscreteStringKeyFrame Value="Animated Text" KeyTime="0:0:7" /> 
      </StringAnimationUsingKeyFrames>    
     </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
    </Button.Triggers> 
</Button> 
5

試試這個:

label1.Content = "waiting..."; 

label1.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate() 
    { 

     label1.UpdateLayout(); 
    })); 

System.Threading.Thread.Sleep(3000); 
label1.Content = "done!"; 
+0

是否有()這項建議,並簡單地調用'label1.UpdateLayout任何區別; '不用調度員? – user181813

+1

是的,只需調用UpdateLayout就行不通。該方法立即返回,並在稍後的UI線程中調度更新(可能在Button Click事件完成時)。然後,線程休眠3秒鐘,並安排另一次更改,當Button Click方法結束時,UI線程最終呈現視圖。 在Windows窗體中,您可以只使用Label上的Update方法,但WPF的工作方式不同。 –