【问题标题】:C# WPF Animated label content is not being shownC# WPF 动画标签内容未显示
【发布时间】:2017-12-14 14:19:47
【问题描述】:

我正在尝试为标签的内容制作动画以执行以下操作:

Loading
Loading.
Loading..
Loading...

再次:

Loading
Loading.
Loading..
Loading...

诸如此类example

代码下方:

<Label Grid.Row="2" x:Name="CurrentTask" 
       FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic" FontWeight="Bold" 
       Foreground="White" Content="Loading">
    <Label.Template>
        <ControlTemplate>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Label.Template>
</Label>

问题是标签内容在 wpf 窗口中不可见。它没有被显示。我做错了什么?

同样在类型的行中:

<DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>

我想将标签内容连接到点而不是硬编码值,我该怎么做?我不想在每个DiscreteObjectKeyFrame 中重复“加载”字符串。

更新: 例如在应用标签样式的情况下,我使用了 label.loaded,而不是在 IsEnabled=True 上引发触发器:

    <Label Grid.Row="2" x:Name="CurrentTask" 
           FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic"  
           Foreground="White" Content="Loading">
        <Label.Style>
            <Style TargetType="Label">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Label.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>

【问题讨论】:

    标签: c# wpf storyboard label controltemplate


    【解决方案1】:

    除了触发器,你的 ControlTemplate 是空的,所以显然什么都没有显示。

    您可以添加 ContentPresenter,也可以删除 Storyboard.TargetName。您的 Trigger 作用于 IsEnabled 设置为 false 看起来也很奇怪。

    <Label.Template>
        <ControlTemplate TargetType="Label">
    
            <ContentPresenter/>
    
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Label.Template>
    

    但是,您实际上可能想要的是样式触发器而不是 ControlTemplate 触发器:

    <Label ...>
        <Label.Style>
            <Style TargetType="Label">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
    

    对于Content中重复的“Loading”字符串,只需使用两个Label,一个带有固定的“Loading”字符串,另一个带有点,并调整它们的Padding。或者更好的是,两个 TextBlock:

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Loading"/>
        <TextBlock>
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text" Duration="00:00:00.8" RepeatBehavior="Forever">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value=""/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="."/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value=".."/>
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="..."/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </StackPanel>
    

    【讨论】:

    • 嗨克莱门斯!为了进行快速测试,我尝试了您的第二个解决方案,设置标签样式触发器,但它不起作用。标签内容始终保持“正在加载”。我有一个带有窗口和这个标签的 MVVM 应用程序,这个窗口是一个闪屏。这个闪屏是从视图构造函数中调用的(通过实例化构造函数)。最后通过调用 Show() 显示。所有这些东西都在使用一个将公寓状态设置为 STA 的线程进行......
    • ...与此同时,在视图构造函数中,流程继续,我使用视图模型设置数据上下文,然后在视图模型构造函数中执行数据库请求。完成后,启动屏幕将关闭。也许是由于线程不工作?
    • 正如我所说,您的 Trigger 对 IsEnabled 设置为 false 的行为看起来很奇怪。这是故意的吗?
    • 在所有示例中,我已将其更改为 &lt;Trigger Property="IsEnabled" Value="True"&gt;,因为在 False 上触发似乎没有意义。
    • 你好克莱门斯!您的解决方案就像一个魅力!以前帖子中评论的问题是由于线程。我没有很好地管理它。谢谢!
    猜你喜欢
    • 2020-12-26
    • 2012-08-24
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多