2014-07-16 76 views
0

我正在用Visual Studio 2012製作C#wpf應用程序。它動畫去掉4秒。它將鼠標移到此處我想暫停動畫。C#WPF在故事板中暫停動畫窗口

我該如何做到這一點?

<Window x:Class="Exmaple.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
Title="Notification Popup" Width="300" SizeToContent="Height" 
WindowStyle="None" AllowsTransparency="True" Height="Auto" Background="Transparent"> 

<Grid x:Name="abc" RenderTransformOrigin="0,1" Height="Auto" Width="300" Margin="0,0,0,0" MouseEnter="Grid_MouseEnter_1" > 

    <!-- Notification area --> 
    <Border BorderThickness="1" Background="#FF2D2D30" BorderBrush="Black" CornerRadius="0" Margin="0,0,0,0"> 
     <!--StackPanel Margin="20"--> 
     <TextBlock x:Name="textblocknotify" TextWrapping="Wrap" Height="Auto" Margin="5" Foreground="White"> 

      </TextBlock> 
      <!--CheckBox Content="Checkable" Margin="5 5 0 5" /--> 
      <!--Button Content="Clickable" HorizontalAlignment="Center" /--> 
     <!--/StackPanel--> 
    </Border> 
    <!-- Animation --> 



    <Grid.Triggers > 
     <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
      <BeginStoryboard> 
       <Storyboard Completed="Storyboard_Completed_1"> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> 
         <SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/> 
        </DoubleAnimationUsingKeyFrames> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="0:0:4.5" Value="1"/> 
         <SplineDoubleKeyFrame KeyTime="0:0:5" Value="0"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
      <BeginStoryboard> 
       <Storyboard Completed="Storyboard_Completed_1"> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> 
         <SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/> 
        </DoubleAnimationUsingKeyFrames> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="0:0:4.5" Value="1"/> 
         <SplineDoubleKeyFrame KeyTime="0:0:5" Value="0"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </Grid.Triggers> 



    <Grid.RenderTransform> 
     <ScaleTransform ScaleY="1" /> 
    </Grid.RenderTransform> 


</Grid> 

這背後

public partial class Window1 : Window 
{ 
    public Window1(String s) 
    { 
     InitializeComponent(); 
     textblocknotify.Text = s; 
     Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => 
     { 

      var workingArea = System.Windows.SystemParameters.WorkArea; 
      var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice; 
      var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom)); 

      this.Left = corner.X - this.ActualWidth - 10; 
      this.Top = corner.Y - this.ActualHeight-30; 
     })); 

     //this.Close(); 
    } 

    private void Storyboard_Completed_1(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void Grid_MouseEnter_1(object sender, MouseEventArgs e) 
    { 
     //don't know what to do 

    } 

回答

1

代碼可以使用PauseStoryboard ClassUIElement.MouseEnter Event暫停運行Animation。同樣,如果您希望Animation在鼠標不在控件上時恢復,則可以使用ResumeStoryboard ClassUIElement.MouseLeave Event。這裏有一個簡單的例子來演示:

<Button Content="Click Me"> 
    <Button.Triggers> 
     <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
      <BeginStoryboard Name="OpacityStoryboard"> 
       <Storyboard> 
        <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)" 
         From="0" To="1" RepeatBehavior="Forever" AutoReverse="True" /> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="UIElement.MouseEnter"> 
      <PauseStoryboard BeginStoryboardName="OpacityStoryboard" /> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="UIElement.MouseLeave"> 
      <ResumeStoryboard BeginStoryboardName="OpacityStoryboard" /> 
     </EventTrigger> 
    </Button.Triggers> 
</Button> 
+0

謝謝你的結構良好的答案。完美的工作。 – user3693167