2017-04-02 46 views
1

在我的項目中,程序啓動時多邊形正在設置路徑上移動 當我的鼠標光標移動時(當鼠標光標離開多邊形,多邊形必須繼續移動時)WPF c#XAML ismouseover animation

<Window x:Class="aqua3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:aqua3" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <PathGeometry x:Key="pathg"> 
     <PathFigure IsClosed="True"> 
      <PolyLineSegment Points="0,0 200,0 200,80 0,80" /> 
     </PathFigure> 
    </PathGeometry> 
</Window.Resources> 
<Grid Background="LightBlue"> 
    <Canvas Margin="10" Background="LightBlue"> 
     <Path Stroke="Red" Data="{StaticResource pathg}" Canvas.Top="10" Canvas.Left="10" /> 
     <Polygon Name="polygon1" Stroke="Yellow" Fill="Yellow" Points="100,150 120,140 120,120 165,150 120,180 120,160 "> 
      <Polygon.Triggers> 
       <EventTrigger RoutedEvent="Window.Loaded"> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimationUsingPath Storyboard.TargetProperty="(Canvas.Top)" 
            Duration="0:0:15" RepeatBehavior="Forever" 
            PathGeometry="{StaticResource pathg}" Source="Y" > 
          </DoubleAnimationUsingPath> 
          <DoubleAnimationUsingPath Storyboard.TargetProperty="(Canvas.Left)" 
            Duration="0:0:15" RepeatBehavior="Forever" 
            PathGeometry="{StaticResource pathg}" Source="X" > 
          </DoubleAnimationUsingPath> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
      </Polygon.Triggers> 
     </Polygon> 
    </Canvas> 
</Grid> 

回答

1
  1. 指定名稱的BeginStoryboard

    <BeginStoryboard x:Name="Move"> 
    
  2. 要暫停,添加此

    <EventTrigger RoutedEvent="MouseEnter"> 
        <PauseStoryboard BeginStoryboardName="Move"/> 
    </EventTrigger> 
    
  3. 要恢復,添加此

    <EventTrigger RoutedEvent="MouseLeave"> 
        <ResumeStoryboard BeginStoryboardName="Move"/> 
    </EventTrigger> 
    
+0

非常感謝你:) – Pewbot