我在运行时在框架元素上应用DoubleAnimation
。以下是代码段:
foreach (FrameworkElement objInnerElement in objFrameWorkElement)
{
Point ElementPosition = new Point(Canvas.GetLeft(objInnerElement), Canvas.GetTop(objInnerElement));
DoubleAnimation db = new DoubleAnimation();
//db.From = objMoveIn.From;
db.AutoReverse = objMoveIn.AutoReverse;
if (objMoveIn.Repeat)
db.RepeatBehavior = RepeatBehavior.Forever;
db.Duration = TimeSpan.FromSeconds(objMoveIn.AnimationDuration);
Storyboard story = new Storyboard();
if (objMoveIn.ddlDirection.SelectedIndex == 0)
{
//(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)
Storyboard.SetTargetProperty(db, new PropertyPath("(FrameworkElement.RenderTransform).(TranslateTransform.X)"));
//Storyboard.SetTargetProperty(db, new PropertyPath(TranslateTransform.XProperty));
db.From = 0 - ElementPosition.X;
db.To = 0;
}
else
{
Storyboard.SetTargetProperty(db, new PropertyPath("(FrameworkElement.RenderTransform).(TranslateTransform.Y)"));
//Storyboard.SetTargetProperty(db, new PropertyPath(TranslateTransform.YProperty));
db.From = 0 - ElementPosition.X;
db.To = 0;
}
story.Children.Add(db);
BeginStoryboard beginstoryboard = new BeginStoryboard();
beginstoryboard.Storyboard = story;
TranslateTransform oTransform = objInnerElement.RenderTransform as TranslateTransform;
if (oTransform == null)
{
oTransform = new TranslateTransform();
oTransform.X = 0;
oTransform.Y = 0;
objInnerElement.RenderTransform = oTransform;
}
if (objMoveIn.ddlDirection.SelectedIndex == 0)
oTransform.BeginAnimation(TranslateTransform.XProperty, db);
else
oTransform.BeginAnimation(TranslateTransform.YProperty, db);
Style objStyle = new Style();
EventTrigger objEventTrigger = new EventTrigger(FrameworkElement.LoadedEvent);
objEventTrigger.Actions.Add(beginstoryboard);
objStyle.Triggers.Add(objEventTrigger);
objInnerElement.Style = objStyle;
}
动画成功运作但我的问题是当我尝试从这个应用的框架元素生成XAML时
string shapestring = XamlWriter.Save(element);
StringReader stringReader = new StringReader(shapestring);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
Element DeepCopyobject = (Element)XamlReader.Load(xmlTextReader);
它会抛出错误,如下所示:
{“无法转换字符串'RenderTransform。(TranslateTransform.X)' 在属性'TargetProperty'中 对象类型 'System.Windows.PropertyPath'。 属性路径无效。不能 解析类型名称 'TranslateTransform'。对象出错 'System.Windows.Media.Animation.DoubleAnimation',
请帮帮我。
答案 0 :(得分:0)
我无法准确地重复一个问题,但是当我有这样的标记时:
<Canvas x:Name="canvas"
Grid.Row="1"
Grid.ColumnSpan="3">
<Rectangle Canvas.Left="10" Canvas.Top="10" Width="50"
Height="10"
RadiusX="5"
RadiusY="5"
Stroke="Black" Fill="AliceBlue" />
<Rectangle Canvas.Left="30"
Canvas.Top="100"
Width="50"
Height="10"
RadiusX="5"
RadiusY="5"
Stroke="Black"
Fill="Blue" />
<Rectangle Canvas.Left="200"
Canvas.Top="150"
Width="50"
Height="10"
RadiusX="5"
RadiusY="5"
Stroke="Black"
Fill="Green" />
</Canvas>
和这样的代码:
foreach (FrameworkElement objInnerElement in canvas.Children)
{
Point ElementPosition = new Point(Canvas.GetLeft(objInnerElement), Canvas.GetTop(objInnerElement));
DoubleAnimation db = new DoubleAnimation();
db.RepeatBehavior = RepeatBehavior.Forever;
db.Duration = TimeSpan.FromSeconds(1000.0);
Storyboard story = new Storyboard();
Storyboard.SetTargetProperty(db, new PropertyPath("(FrameworkElement.RenderTransform).(TranslateTransform.X)"));
db.From = 0 - ElementPosition.X;
db.To = 0;
story.Children.Add(db);
BeginStoryboard beginstoryboard = new BeginStoryboard();
beginstoryboard.Storyboard = story;
TranslateTransform oTransform = objInnerElement.RenderTransform as TranslateTransform;
if (oTransform == null)
{
oTransform = new TranslateTransform();
oTransform.X = 0;
oTransform.Y = 0;
objInnerElement.RenderTransform = oTransform;
}
oTransform.BeginAnimation(TranslateTransform.XProperty, db);
Style objStyle = new Style();
EventTrigger objEventTrigger = new EventTrigger(FrameworkElement.LoadedEvent);
objEventTrigger.Actions.Add(beginstoryboard);
objStyle.Triggers.Add(objEventTrigger);
objInnerElement.Style = objStyle;
}
string shapestring = XamlWriter.Save(canvas);
StringReader stringReader = new StringReader(shapestring);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
UIElement DeepCopyobject = (UIElement)XamlReader.Load(xmlTextReader);
它工作正常,没有错误。