繁体   English   中英

在wpf中删除情节提要中的问题

[英]issue in Deleting Storyboard in wpf

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="QWERTY.animation"
x:Name="Window"
Title="animation"
Width="640" Height="480">
<Window.Resources>
    <Storyboard x:Key="Animation">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.FontSize)" Storyboard.TargetName="btn_1">
            <EasingDoubleKeyFrame KeyTime="0" Value="11"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="10.667"/>
        </DoubleAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.FontWeight)" Storyboard.TargetName="btn_1">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <FontWeight>Normal</FontWeight>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
            <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                <DiscreteObjectKeyFrame.Value>
                    <FontWeight>Bold</FontWeight>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="btn_1">
            <EasingColorKeyFrame KeyTime="0" Value="#FFEAF0ED"/>
            <EasingColorKeyFrame KeyTime="0:0:0.3" Value="#FF106285"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button x:Name="btn_2" Content="B" Height="55" Margin="236,98,0,0" VerticalAlignment="Top" Click="btn_2_Click" HorizontalAlignment="Left" Width="72" />
    <Button x:Name="btn_1" Content="A" Height="55" Margin="115,98,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="72" Click="btn_1_Click">
        <Button.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="White" Offset="0"/>
                <GradientStop Color="#FFEAF0ED" Offset="0.9"/>
            </LinearGradientBrush>
        </Button.Background>
    </Button>
    <Button Content="Remove" HorizontalAlignment="Left" Margin="173,179,0,217" Width="75" Click="Button_Click" />
</Grid>

The design will be like below

在此处输入图片说明

public partial class animation : Window
        {
    Storyboard SB;
    public animation()
    {
        this.InitializeComponent();         
    }

    public void Animate(string name)
    {
        SB = (Storyboard)(FindResource("Animation"));
        Storyboard.SetTargetName(SB.Children[0], name);
        Storyboard.SetTargetName(SB.Children[1], name);
        Storyboard.SetTargetName(SB.Children[2], name);
        SB.Begin();            
    }
    private void btn_1_Click(object sender, RoutedEventArgs e)
    {
        Button Btn_1 = (Button)sender;
        Animate(btn_1.Name);
    }

    private void btn_2_Click(object sender, RoutedEventArgs e)
    {
        Button Btn_2 = (Button)sender;
        Animate(Btn_2.Name);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SB.Remove();
    }
}

它的输出将如下所示

产量

上面的结果是我一个接一个地单击两个按钮之后。然后单击了删除按钮,只删除了按钮B动画。 但是我想通过单击删除按钮来删除按钮A和按钮B的动画

一次只能将一个TargetName分配给Storyboard。 通过使用SetTargetName,您可以将目标转移到新按钮。 然后,当您单击删除时,将删除您添加的最后一个。 看一下它的SilverLight 博客 ,但应在此处申请。 您唯一的其他选择是使用Style.Triggers并将动画放入Style中。

从上方链接:

在此示例中,可能不希望在一个矩形上停止动画以使动画可以在另一个矩形上开始。 也许您希望两个动画同时运行。 但是,您不能使用同一动画对象同时运行两个单独的动画,因为只有一个TargetName。 这并不意味着您要回到为每个对象创建单独的情节提要。 相反,对于要同时(同步)运行的每个动画,都需要一个Storyboard。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM