2012-07-13 108 views
1
<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> 

問題刪除故事板在WPF

enter image description here

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(); 
    } 
} 

它的輸出會像下面

Output

上述結果是在我點擊兩個按鈕之後。然後我點擊刪除按鈕,只刪除動畫中的按鈕B。但我想刪除按鈕A和按鈕B動畫通過點擊刪除按鈕。

回答

1

分配給故事板的時間只能有一個TargetName。通過使用SetTargetName,您可以將目標轉移到新的按鈕。然後,當你點擊刪除,你刪除你添加的最後一個。看看這個Silverlight的Blog,但應該在這裏適用。您唯一的選擇是使用Style.Triggers並將您的動畫放在樣式中。

從以上鍊接:

在這個例子中,它可能是不希望爲在一個矩形停止的動畫,使得動畫可以在另一個矩形啓動。也許你想讓兩個動畫同時運行。但是,您不能使用同一個動畫對象同時運行兩個獨立的動畫,因爲只有一個TargetName。這並不意味着您要重新爲每個對象創建一個單獨的Storyboard。相反,您需要爲要同時運行(同步)的每個動畫創建一個Storyboard。