2010-03-03 61 views
0

我試圖用Silverlight 4創建一個簡單的平移和縮放應用程序,但是Im無法更新TranslateTransform和ScaleTransform屬性之後我對它們運行了動畫。Silverlight在動畫運行後無法更新屬性

我試圖設置FillBehaviour停止,但沒有成功。

這裏是我的代碼:

<Canvas x:Name="LayoutRoot" Background="White" Width="800" Height="600"> 
     <Canvas x:Name="PanningCanvas" Height="600" Width="800"> 
      <Canvas.RenderTransform> 
       <TransformGroup> 
        <ScaleTransform x:Name="CanvasScale"/> 
        <SkewTransform/> 
        <RotateTransform/> 
        <TranslateTransform x:Name="CanvasTransform"/> 
       </TransformGroup> 
      </Canvas.RenderTransform> 
      <Rectangle x:Name="MovingRect" Fill="#FF834040" Height="209" Canvas.Left="219" Stroke="#FF500505" Canvas.Top="220" Width="329" StrokeThickness="2"/> 
     </Canvas> 
     <Button Content="Animate" Width="107" Canvas.Left="38" Canvas.Top="46" ClickMode="Press" Click="Button_Click"/> 
    </Canvas> 

,這裏是這一切(略有刪節)

public partial class MainPage : UserControl 
    { 
     private ManipulationProcessor2D manipulationProcessor; 

     public MainPage() 
     { 
      InitializeComponent(); 

      this.manipulationProcessor = new ManipulationProcessor2D(Manipulations2D.Translate | Manipulations2D.Scale); 

      this.manipulationProcessor.Delta += OnManipulationDelta; 

      Touch.FrameReported += OnCapturedTouchReported; 
     } 

     private void OnManipulationDelta(object sender, Manipulation2DDeltaEventArgs e) 
     { 
      float zoomVal = e.Delta.ScaleX; 
      Point pinchPoint = new Point(e.OriginX, e.OriginY); 

      float xTranslation = e.Delta.TranslationX; 
      float yTranslation = e.Delta.TranslationY; 

      if (zoomVal != 1.0) 
      { 
       Zoom(zoomVal, PanningCanvas.RenderTransform.Inverse.Transform(pinchPoint), pinchPoint, xTranslation, yTranslation, true); 
      } 
      else if (xTranslation != 0 || yTranslation != 0) 
      { 
       Translate(xTranslation, yTranslation); 
      } 
     } 

     public void Zoom(double zoom, Point pinchPosition, Point physicalPosition, float xTranslation, float yTranslation, bool isFinger) 
     { 
      if (isFinger) 
      { 
       CanvasScale.ScaleX = CanvasScale.ScaleX * zoom; 
       CanvasScale.ScaleY = CanvasScale.ScaleY * zoom; 

       CanvasTransform.X = -1 * (pinchPosition.X * CanvasScale.ScaleX - physicalPosition.X); 
       CanvasTransform.Y = -1 * (pinchPosition.Y * CanvasScale.ScaleY - physicalPosition.Y); 
      } 
      else 
      { 
       CanvasScale.ScaleX = CanvasScale.ScaleX + zoom; 
       CanvasScale.ScaleY = CanvasScale.ScaleY + zoom; 
      } 
     } 

     private void Translate(float xTranslation, float yTranslation) 
     { 
      CanvasTransform.X += xTranslation; 
      CanvasTransform.Y += yTranslation; 
     } 

     private void OnCapturedTouchReported(object sender, TouchFrameEventArgs e) 
     { 
      //..removed..// 

      // process manipulations 
      this.manipulationProcessor.ProcessManipulators(DateTime.UtcNow.Ticks,manipulators); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      AnimatePosition(new Point(400, 300), 1); 
      AnimateScale(1.2, 1); 
     } 

     private Storyboard translateStoryboard = new Storyboard(); 
     private Storyboard scaleStoryboard = new Storyboard(); 

     public void AnimatePosition(Point destinationPoint, double lengthOfAnimation) 
     { 
      Point offset = new Point(destinationPoint.X, destinationPoint.Y); 

      var translationAnimationX = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = offset.X }; 
      translateStoryboard.Children.Add(translationAnimationX); 

      Storyboard.SetTargetProperty(translationAnimationX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)")); 

      var translationAnimationY = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = offset.Y }; 
      translateStoryboard.Children.Add(translationAnimationY); 

      Storyboard.SetTargetProperty(translationAnimationY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)")); 
      Storyboard.SetTarget(translateStoryboard, PanningCanvas); 

      translateStoryboard.Begin(); 
     } 

     public void AnimateScale(double scale, double lengthOfAnimation) 
     { 
      var scaleAnimationX = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = scale }; 
      scaleStoryboard.Children.Add(scaleAnimationX); 

      Storyboard.SetTargetProperty(scaleAnimationX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)")); 

      var scaleAnimationY = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = scale }; 
      scaleStoryboard.Children.Add(scaleAnimationY); 

      Storyboard.SetTargetProperty(scaleAnimationY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)")); 
      Storyboard.SetTarget(scaleStoryboard, PanningCanvas); 

      scaleStoryboard.Begin(); 
     } 
    } 

問題的背後是C#,我按一下按鈕後,我做不到翻譯或放大了...

請幫忙!

馬克

回答

0

好吧,所以我得到它的工作,但我不知道爲什麼。

更改動畫這樣的工作:

 Storyboard translateStoryboard = new Storyboard(); 
     Point offset = new Point(destinationPoint.X, destinationPoint.Y); 

     var translationAnimationX = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = offset.X }; 
     var translationAnimationY = new DoubleAnimation() { SpeedRatio = 1, Duration = new Duration(TimeSpan.FromSeconds(lengthOfAnimation)), To = offset.Y }; 

     Storyboard.SetTargetProperty(translationAnimationX, new PropertyPath("X")); 
     Storyboard.SetTargetProperty(translationAnimationY, new PropertyPath("Y")); 

     Storyboard.SetTargetName(translationAnimationX, "CanvasTransform"); 
     Storyboard.SetTargetName(translationAnimationY, "CanvasTransform"); 

     translateStoryboard.Children.Add(translationAnimationX); 
     translateStoryboard.Children.Add(translationAnimationY); 

     translateStoryboard.Begin(); 

的伎倆,但我不知道爲什麼...

可有人請解釋一下?