简体   繁体   English

如何克隆动画笔刷?

[英]How to Clone animated Brush?

I have an animated Brush object and i want to Clone this brush. 我有一个动画的Brush对象,我想克隆此画笔。

ColorAnimation ani = new ColorAnimation(Colors.White, TimeSpan.FromSeconds(1))
{ RepeatBehavior = RepeatBehavior.Forever, AutoReverse = true };

SolidColorBrush brush1 = new SolidColorBrush(Colors.Black);
brush1.BeginAnimation(SolidColorBrush.ColorProperty, ani);

SolidColorBrush brush2 = brush1.Clone();

// brush2 is not an animated Brush

if (!brush2.HasAnimatedProperties)
    MessageBox.Show("I don't want this!");

As MSDN Library says (Brush.Clone method): 正如MSDN库所说(Brush.Clone方法):

Creates a modifiable clone of this Brush, making deep copies of this object's values. 创建此Brush的可修改克隆,从而复制该对象的值。 When copying dependency properties, this method copies resource references and data bindings (but they might no longer resolve) but not animations or their current values. 复制依赖项属性时,此方法复制资源引用和数据绑定(但它们可能不再解析),但不复制动画或其当前值。

So, what's the best way to clone my animated brush? 那么,克隆动画画笔的最佳方法是什么? Thanks. 谢谢。

A solid approach could be extending the Brush class and implement ICloneable in your derived class. 一种可靠的方法可能是扩展Brush类并在派生类中实现ICloneable

Your custom Clone method can then handle the cloning of whatever you need to be cloned. 然后,您的自定义Clone方法可以处理任何您需要克隆的克隆。

You need to recreate the animation on the clone. 您需要在克隆上重新创建动画。 There is no other way. 没有别的办法了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM