XAML 中的动画省略号

Animating ellipsis in XAML

想象一些文字:

<TextBlock>Loading...</TextBlock>

我想要一个简单的省略号动画(... 字符),它在 ...... 之间缓慢循环为了给人一种有事发生的印象。

在 WPF XAML 中有没有一种简单的方法可以做到这一点?

纯 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>

如果你需要一些东西,你可以沙箱和玩。

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>

免责声明: 不是一个完整的例子,有一个可以取消的正式后台线程,但你可以通过一些努力让它从你正在加载的内容等中报告信息。

对于那些简单的 XAML 动画是不够的场景。