隐藏边框或文本块的动画

本文关键字:动画 文本 边框 隐藏 | 更新日期: 2023-09-27 18:14:45

我如何隐藏和返回元素,如边界或TextBlock(当我点击它)与动画?我现在通过改变"点击"事件的不透明度值(从0到1,反之亦然)来做到这一点。当我第一次点击它时,它消失了,当我再次点击它时,它出现了,但它没有动画。我想添加一些动画,当元素逐渐消失。我知道有"fadeoutthemeanimation",但我不能根据需要配置它

<Border Tapped="Border_Tapped" Background="#A5000000" Height="80">
    <TextBlock Text="Test"/>
</Border>
private void Border_Tapped(object sender, TappedRoutedEventArgs e)
{
    Border b = (Border)sender;
    b.Opacity = b.Opacity == 0.0 ? 1.0 : 0.0;
}

我添加了fadeoutthemeanation,它像我想要的那样工作,但是我应该按住border。点击边框,开始动画

<StackPanel>
    <StackPanel.Resources>
        <Storyboard SpeedRatio="0.1" x:Name="EnterStoryboard">
            <FadeOutThemeAnimation Storyboard.TargetName="border" />
        </Storyboard>
        <Storyboard SpeedRatio="0.1" x:Name="ExitStoryboard">
            <FadeInThemeAnimation Storyboard.TargetName="border" />
        </Storyboard>
    </StackPanel.Resources>
    <Border Name="border" Tapped="Border_Tapped" Background="#A5000000" Height="80" 
            HorizontalAlignment="Center" VerticalAlignment="Center"
            PointerEntered="Border_PointerEntered" 
           PointerExited="Border_PointerExited">
         <TextBlock Text="Test" FontSize="40" TextWrapping="Wrap" Foreground="White"/>
    </Border>
</StackPanel>
    private void Border_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Border b = (Border)sender;
        b.Opacity = b.Opacity == 0.0 ? 1.0 : 0.0;
    }
    private void Border_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
        EnterStoryboard.Begin();
    }
    private void Border_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        ExitStoryboard.Begin();
    }

隐藏边框或文本块的动画

假设你的TextBlock是这个

<TextBlock x:Name="textBlock" Text="Fade Me" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Tap="textBlock_Tap" />

你可以很容易地在blend中做到这一点。将以下XAML代码添加到包含textblock的页面:

<phone:PhoneApplicationPage.Resources>
    <Storyboard x:Name="FadeOutTextBlock">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBlock">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Name="FadeInTextBlock">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBlock">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</phone:PhoneApplicationPage.Resources>

然后在Tap事件处理程序中添加以下代码

private void textBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
     if (textBlock.Opacity == 0.0)
     {
         FadeInTextBlock.Begin();
     }
     else
     {
         FadeOutTextBlock.Begin();
     }
}

现在运行应用程序并点击TextBlock。:)希望有帮助。