【问题标题】:how to fix partially working uwp storyboard animation如何修复部分工作的 uwp 故事板动画
【发布时间】:2019-02-11 16:56:57
【问题描述】:

在 UWP 中,我实现了 ToggleButton 的视觉状态,尝试为其子控件的高度和前景等设置动画,但只有一种颜色动画有效,其余的并没有按预期更改值。但是,如果我将这些动画重写为设置器,它们就起作用了。我很确定我没有输入错误的值,否则会引发错误。这个 controlTemplate 也是扩展器的 headerStyle 模板的内容。

<ControlTemplate TargetType="ToggleButton">
    <Grid
        x:Name="ToggleButton"
        Margin="0,0"
        Padding="0"
        Background="Transparent"
        BorderBrush="{StaticResource mediumText}"
        BorderThickness="1"
        CornerRadius="5">
        <RelativePanel x:Name="InnerExpanderRelativePanel" Margin="0">
            <RelativePanel.Background>
                <SolidColorBrush x:Name="InnerExpanderRelativePanelColor" Color="White" />
            </RelativePanel.Background>
            <RelativePanel Background="Transparent" RelativePanel.AlignVerticalCenterWithPanel="True">
                <BitmapIcon
                    x:Name="OldApprovalIcon"
                    Width="30"
                    Height="30"
                    Margin="20,0,0,0"
                    RelativePanel.AlignLeftWithPanel="True"
                    UriSource="{StaticResource drawable/approval_detail_expired}">
                    <BitmapIcon.Foreground>
                        <SolidColorBrush
                            x:Name="IconColor"
                            Opacity="0.5"
                            Color="White" />
                    </BitmapIcon.Foreground>
                </BitmapIcon>
                <BitmapIcon
                    x:Name="OldApprovalIcon2"
                    Width="30"
                    Height="30"
                    Margin="20,0,0,0"
                    Opacity="0.5"
                    RelativePanel.AlignHorizontalCenterWith="OldApprovalIcon"
                    RelativePanel.AlignVerticalCenterWith="OldApprovalIcon"
                    UriSource="{StaticResource drawable/approval_detail_expired}">
                    <BitmapIcon.Foreground>
                        <SolidColorBrush
                            x:Name="IconColor2"
                            Opacity="0.5"
                            Color="{StaticResource rsvpRed}" />
                    </BitmapIcon.Foreground>
                </BitmapIcon>
                <TextBlock
                    Margin="20,0,0,0"
                    FontSize="15"
                    FontWeight="Medium"
                    Foreground="{StaticResource rsvpRed}"
                    RelativePanel.AlignVerticalCenterWithPanel="True"
                    RelativePanel.RightOf="OldApprovalIcon"
                    Text="show 29 expired eSignatures" />
            </RelativePanel>
        </RelativePanel>
        <!--  animate expander header  -->
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Pressed">
                    <VisualState.Setters>
                        <Setter Target="IconColor.Color" Value="Black" />
                        <Setter Target="ToggleButton.Margin" Value="7" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Checked">
                    <VisualState.Setters>
                        <Setter Target="ToggleButton.Margin" Value="0" />
                    </VisualState.Setters>
                    <Storyboard>
               <!--ONLY THIS WORKS-->
                        <ColorAnimation
                            Storyboard.TargetName="InnerExpanderRelativePanel"
                            Storyboard.TargetProperty="(RelativePanel.Background).(SolidColorBrush.Color)"
                            To="{StaticResource rsvpRed}"
                            Duration="0:0:0.2" />
               <!--THESE THREE DON'T WORK-->
                        <DoubleAnimation
                            Storyboard.TargetName="InnerExpanderRelativePanel"
                            Storyboard.TargetProperty="Height"
                            To="50"
                            Duration="0:0:0.2" />
                        <ColorAnimation
                            Storyboard.TargetName="IconColor"
                            Storyboard.TargetProperty="Color"
                            To="White"
                            Duration="0:0:0.2" />
                        <ColorAnimation
                            Storyboard.TargetName="OldApprovalIcon2"
                            Storyboard.TargetProperty="(BitmapIcon.Foreground).(SolidColorBrush.Color)"
                            To="White"
                            Duration="0:0:0.2" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</ControlTemplate>

【问题讨论】:

  • 您的问题解决了吗?

标签: .net xaml uwp


【解决方案1】:

您可以使用ObjectAnimationUsingKeyFrames 来解决此问题。

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerExpanderRelativePanel" Storyboard.TargetProperty="Height">
     <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="50"/>
</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="IconColor" Storyboard.TargetProperty="Color">
     <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="Yellow"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="IconColor2" Storyboard.TargetProperty="Color">
     <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="Green"/>
</ObjectAnimationUsingKeyFrames>

【讨论】:

  • 不幸的是,添加此行后它仍然无法正常工作,但是我设法用 伪造它,因为它是一个如此短的动画。我不确定为什么其他两个 colorAnimations 也不起作用。
  • @ShiboXing 我发现使用ObjectAnimationUsingKeyFrames 可以解决这个问题。请尝试一下。
  • 它作为一个没有过渡的设置器工作,但我觉得 ObjectAnimationUsingKeyFrames 是正确的使用方法。我得进一步调查。非常感谢!
  • 刚发现LinearDoubleKeyFrame 和DoubleAnimationUsingKeyFrames 也不起作用,奇怪的是任何涉及过渡的东西都不起作用
猜你喜欢
  • 2012-02-21
  • 1970-01-01
  • 2021-05-05
  • 2020-05-19
  • 1970-01-01
  • 2011-07-20
  • 2010-10-14
  • 2013-01-25
相关资源
最近更新 更多