Ben*_*osh 3 c# wpf animation visibility storyboard
我通过将StrokeDashOffset动画应用于Rectangle控件来实现"行进蚂蚁"样式动画.我希望动画在矩形可见时播放,但在隐藏时不占用额外的CPU周期.WPF足够智能,可以在隐藏受影响的控件时自动暂停动画吗?
没有.WPF足够聪明,不能这样做:).这背后的原因是你无法预测动画属性的作用(它可以是任何依赖属性,与控件外观无关).
您可以进行以下测试.
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>
Run Code Online (Sandbox Code Playgroud)
C#:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您会注意到调试输出中WPF亮度的证明:DoubleProperty无论控件是否可见,它都会显示更改,但是在测量/排列方面,可见性很重要.虽然我将DoubleProperty标记为影响meausre并安排的属性,但是在控制折叠时不会调用处理程序.
| 归档时间: |
|
| 查看次数: |
2351 次 |
| 最近记录: |