Does WPF storyboard animation continue when a hidden control is hidden?

I'm implementing a marching ant style animation by applying a StrokeDashOffset animation to a Rectangle control. I would like the animation to play when the rectangle is visible, but it does not capture the extra processor cycles. Is WPF smart enough to automatically pause animation when a hidden control is hidden?

+3
source share
2 answers

No. WPF is smart enough not to do this :). The reason for this is that you cannot predict what an animated property is (it can be any dependency property that is not related to the appearance of the control).

You can perform the following test.

XAML:

<Window x:Class="WpfApplication1.TestBrowser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Animation Test"
        Height="300"
        Width="300">
    <StackPanel>
            <Button Content="Toggle label" 
                            Click="ToggleLableClick"/>
            <local:MyLabel x:Name="lbl" Content="Hello" />
    </StackPanel>
</Window>

WITH#

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
  public partial class TestBrowser : Window
  {
    public TestBrowser()
    {
      InitializeComponent();
        var da = new DoubleAnimation(0, 10, new Duration(TimeSpan.FromSeconds(10)))
                    {
                        AutoReverse = true,
                        RepeatBehavior = RepeatBehavior.Forever
                    };
        lbl.BeginAnimation(MyLabel.DoublePropertyProperty, da);
    }

    private void ToggleLableClick(object sender, RoutedEventArgs e)
    {
        lbl.Visibility = lbl.IsVisible ? Visibility.Collapsed : Visibility.Visible;
    }
  }

    public class MyLabel : Label
    {
        public double DoubleProperty
        {
            get { return (double)GetValue(DoublePropertyProperty); }
            set { SetValue(DoublePropertyProperty, value); }
        }

        public static readonly DependencyProperty DoublePropertyProperty =
                DependencyProperty.Register("DoubleProperty", typeof(double), typeof(MyLabel), 
                new FrameworkPropertyMetadata(0.0,
                    FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange, OnDoublePropertyChanged));

        private static void OnDoublePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Trace.WriteLine(e.NewValue);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Trace.WriteLine("Measure");
            return base.MeasureOverride(constraint);
        }

        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            Trace.WriteLine("Arrange");
            return base.ArrangeOverride(arrangeBounds);
        }
    }
}

You'll notice a confirmation of the brightness of WPF in the debug output: it shows the changes DoublePropertyno matter what control is visible or not, but visibility matters when it comes to Measure / Arrange. Handlers do not call when control is minimized, although I have designated the DoubleProperty property as a property that affects meausre and organizes ...

+5
source

I think the animation continues, but the visualization system will understand that the rectangle is invisible and will not waste time redrawing.

, , .

0

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


All Articles