2015-04-14 118 views
0

想象一些文字:動畫效果省略號

<TextBlock>Loading...</TextBlock> 

我想省略號的一個簡單的動畫(在...字符)它在一個緩慢的週期......之間振盪,以便給事情正在發生的印象。

是否有一種簡單的方法可以在XAML中爲WPF做到這一點?

+0

謊言關於實際上意識到加載過程的用戶界面?讓我們看看我能否想到一些事情。 –

回答

5

純XAML的解決方案可能是這樣的:

<TextBlock> 
    <TextBlock.Triggers> 
     <EventTrigger RoutedEvent="Loaded"> 
      <BeginStoryboard> 
       <Storyboard Duration="0:0:3" RepeatBehavior="Forever"> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text"> 
         <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Loading."/> 
         <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="Loading.."/> 
         <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="Loading..."/> 
        </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </TextBlock.Triggers> 
</TextBlock> 
+0

這正是我所追求的,它完美的工作。非常感謝。 –

0

如果你需要的東西,你可以沙盤裏玩。

class LoadingElipsis : INotifyPropertyChanged 
{ 
    public LoadingElipsis() 
    { 
     Thread thread = new Thread(this.Spin); 
     thread.Start(); 
    } 

    public string Display 
    { 
     get 
     { 
      switch(DateTime.Now.Second % 3) 
      { 
       default: return "Loading."; 
       case 1: return "Loading.."; 
       case 2: return "Loading..."; 
      } 
     } 
    } 

    protected void Spin() 
    { 
     while (true) 
     { 
      Thread.Sleep(1000); 
      Notify("Display"); 
     } 
    } 

    protected void Notify(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

而XAML

<Window.Resources> 
    <project:LoadingElipsis x:Key="loading" /> 
</Window.Resources>  
<Grid DataContext="{StaticResource ResourceKey=loading}"> 
    <TextBlock Text="{Binding Display}" Background="Red"/> 
</Grid> 

Disclamer: 不是一個完整的例子,有一個正式的後臺線程,可以取消,但也有一些努力,你可以得到它來報告信息,從你回來」重新加載等。

對於那些簡單的XAML動畫不夠用的場景。

+1

我強烈建議使用計時器(最好是DispatcherTimer)而不是帶睡眠的線程。 – Clemens

+0

我同意,這只是爲了讓它離開地面。 在關鍵幀動畫上使用此方法的唯一原因是如果您必須將其設置爲代碼或具有更多動態效果。 使用此功能(無需睡眠),可以發佈有意義的更新,如%完成等。 –