我需要通过C#为COLLAPSED
属性制作动画。
我有下面的代码几乎正常工作,除了没有崩溃动画。
有任何线索吗?
var myElement = stackObj.Children[n];
Duration d = TimeSpan.FromSeconds(2);
Storyboard sb = new Storyboard() { Duration = d };
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames();
var discreteObjectKeyFrame = new DiscreteObjectKeyFrame()
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))
};
objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame);
Storyboard.SetTarget(objectAnimationUsingKeyFrames, myElement);
Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath("Visibility.Collapsed"));
sb.Children.Add(DA);
string myObjectName = "r" + n;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
我知道在XAML中它应该像
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty = "Opacity"
To = "0"
BeginTime = "0:0:0"
Duration = "0:0:2" />
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty = "Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
但我不知道我必须实现哪些C#代码。
答案 0 :(得分:0)
对于需要这种动画的人,你可以复制/过去代码并且它正在运行! :)
<强> XAML 强>
<StackPanel Background="bLACK" Name="stackObj">
<Rectangle Name="r0" Width="100" Height="50" Fill="#FFDA2121" ></Rectangle>
<Rectangle Name="r1" Width="100" Height="50" Fill="#FFDA2121" ></Rectangle>
<Rectangle Name="r2" Width="100" Height="50" Fill="#FFD8C828" ></Rectangle>
<Rectangle Name="r3" Width="100" Height="50" Fill="#FF17D829" ></Rectangle>
<Rectangle Name="r4" Width="100" Height="50" Fill="#FF0DE080" ></Rectangle>
<Rectangle Name="r5" Width="100" Height="50" Fill="#FF2E5CF9" ></Rectangle>
<Rectangle Name="r6" Height="50" Width="100" Fill="#FFDA2121"/>
<Rectangle Name="r7" Height="50" Width="100" Fill="#FFDA2121"/>
<Rectangle Name="r8" Height="50" Width="100" Fill="#FFDA2121"/>
</StackPanel>
<强> C#强>
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background);
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = TimeSpan.FromSeconds(3);
timer.Tick += timer_Tick;
timer.Start();
sb = new Storyboard();
}
int n = 0;
bool isWorking;
Storyboard sb;
void timer_Tick(object sender, EventArgs e)
{
if (isWorking == false)
{
isWorking = true;
try
{
var myElement = stackObj.Children[n];
var dur = TimeSpan.FromMilliseconds(2000);
var f = CreateVisibility(dur, myElement, false);
sb.Children.Add(f);
Duration d = TimeSpan.FromSeconds(2);
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
sb.Children.Add(DA);
string myObjectName = "r" + n;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + DateTime.Now.TimeOfDay);
}
isWorking = false;
}
}
private static ObjectAnimationUsingKeyFrames CreateVisibility(Duration duration, UIElement element, bool show)
{
var _Two = new DiscreteObjectKeyFrame { KeyTime = new TimeSpan(duration.TimeSpan.Ticks / 2), Value = (show ? Visibility.Visible : Visibility.Collapsed) };
var _Animation = new ObjectAnimationUsingKeyFrames { BeginTime = new TimeSpan(0) };
_Animation.KeyFrames.Add(_Two);
Storyboard.SetTargetProperty(_Animation, new PropertyPath("Visibility"));
Storyboard.SetTarget(_Animation, element);
return _Animation;
}
}