2009-08-20 60 views
2

我在運行時框架元素上應用DoubleAnimation。以下是代碼片段:DoubleAnimation是/錯誤時,試圖產生XAML

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

任何專家?請幫幫我 – Ronak 2009-08-20 10:10:30

回答

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); 

它工作的罰款,沒有錯誤。