想象一下一些文字:
<TextBlock>Loading...</TextBlock>
我喜欢省略号的简单动画(...
字符),它在.
,..
和...
之间以慢速循环播放为了给人一种正在发生的事情的印象。
在XAML for WPF中有一种简单的方法吗?
答案 0 :(得分:6)
纯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>
答案 1 :(得分: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动画还不够的场景。