WinRT 前景动画

本文关键字:动画 WinRT | 更新日期: 2023-09-27 17:56:02

我想对 ContentControl 的前景色进行动画处理。

<VisualStateGroup
x:Name="SelectionStates">
<VisualState
    x:Name="Unselected">
    <Storyboard>
        <DoubleAnimation
            Duration="0"
            Storyboard.TargetName="UnselectedContent"
            Storyboard.TargetProperty="Opacity"
            To="1" />
        <DoubleAnimation
            Duration="0"
            Storyboard.TargetName="Content"
            Storyboard.TargetProperty="Opacity"
            To="0" />
    </Storyboard>
</VisualState>
<VisualState
    x:Name="Selected">
    <Storyboard>
        <DoubleAnimation
            Duration="0"
            Storyboard.TargetName="Content"
            Storyboard.TargetProperty="Opacity"
            To="1" />
        <DoubleAnimation
            Duration="0"
            Storyboard.TargetName="UnselectedContent"
            Storyboard.TargetProperty="Opacity"
            To="0" />
    </Storyboard>
</VisualState>

<ContentControl
    x:Name="Content"
    ContentTemplate="{TemplateBinding ContentTemplate}"
    Foreground="{StaticResource CalendarDayForegroundSelected}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    Margin="{TemplateBinding Padding}"
    IsTabStop="False" >
    <ContentControl.Content>
        <TextBlock Text="{TemplateBinding Content}" />
    </ContentControl.Content>
</ContentControl>
<ContentControl
    x:Name="UnselectedContent"                            
    ContentTemplate="{TemplateBinding ContentTemplate}"
    Foreground="{StaticResource CalendarDayForeground}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    Margin="{TemplateBinding Padding}"
    IsTabStop="False" >
    <ContentControl.Content>
        <TextBlock Text="{TemplateBinding Content}" />
    </ContentControl.Content>
</ContentControl>

这就是我的做法。不幸的是,我需要两次具有不同名称的相同内容控件。有没有更好的方法?

我用ColorAnimation尝试过,但没有成功。

谢谢丹妮

WinRT 前景动画

不能使用 ColorAnimation 直接设置控件的"前景"属性。 但是,您可以通过在 XAML 中显式设置画笔并为其命名来设置其前景画笔的颜色:

<ProgressBar x:Name="ProgressBar" Grid.Row="1" VerticalAlignment="Bottom" Value="{TemplateBinding Progress}">
<ProgressBar.Foreground>
    <SolidColorBrush x:Name="ProgressBrush" Color="Orange"/>
</ProgressBar.Foreground>

现在,您已经有了对控件的前景画笔属性("进度画笔")的引用,您可以使用颜色动画对其颜色进行动画处理。

<ColorAnimation Storyboard.TargetName="ProgressBrush" Storyboard.TargetProperty="Color"
                               To="Black" Duration="0:0:0.25" EnableDependentAnimation="True"/>

希望这有帮助!参考:如何:对纯色画笔的颜色或不透明度进行动画处理