Why is my WPF track animation flickering?

Why does the following animation flicker and act on MouseLeave? If it can be reproduced, I will send a screencast.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas>
    <Path Fill="Blue" Margin="15,15,15,15">
      <Path.Data>
        <!-- Describes an ellipse. -->
        <EllipseGeometry x:Name="MyAnimatedEllipseGeometry" Center="200,100" RadiusX="15" RadiusY="15" />
      </Path.Data>
      <Path.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseEnter">
          <BeginStoryboard>
            <Storyboard Duration="0:0:.5">
              <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" 
                         Storyboard.TargetProperty="RadiusX"
                         From="15" To="100" />
              <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" 
                         Storyboard.TargetProperty="RadiusY"
                         From="15" To="100" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseLeave">
          <BeginStoryboard>
            <Storyboard Duration="0:0:.5">
              <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" 
                         Storyboard.TargetProperty="RadiusX"
                         From="100" To="15" />
              <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" 
                         Storyboard.TargetProperty="RadiusY"
                         From="100" To="15" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Path.Triggers>
    </Path>
  </Canvas>
</Page>
+3
source share
2 answers

The reason is because you are pointing Fromto DoubleAnimations.

If the radius is less than 100 when it MouseLeavehappens, the property Fromwill cause it to jump to 100 and probably cause a MouseEnter. Then you have two competing animations, and the mouse events go crazy when the radius of the ellipse flickers under the cursor.

, From, , , :

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1">
    <Canvas>
        <Path Margin="15,15,15,15" Fill="Blue">
            <Path.Data>
                <!-- Describes an ellipse. -->
                <EllipseGeometry x:Name="MyAnimatedEllipseGeometry" Center="200,100" RadiusX="15" RadiusY="15"/>
            </Path.Data>
            <Path.Triggers>
                <EventTrigger RoutedEvent="UIElement.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusX" To="100"/>
                            <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusY" To="100"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="UIElement.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusX" To="15"/>
                            <DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusY" To="15"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Path.Triggers>
        </Path>
    </Canvas>
</Page>

, Storyboard.Duration, , Storyboard . Duration DoubleAnimation - XAML , .

+3

. , - , . .

100 15 , , , . , 30 .

0

Source: https://habr.com/ru/post/1705000/


All Articles