2012-08-22 68 views
-2

我有圖像內的畫布像以下:WPF改變方向DoubleAnimation是

<Canvas x:Name="cnvMain" ClipToBounds="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SnapsToDevicePixels="True"> 
    <Image x:Name="Image1" Stretch="None" Canvas.Bottom="0" Source="ImageGallery/Desert.jpg" ></Image> 
</Canvas> 

對於上下移動圖像我用DoubleAnimation是類似以下內容:在UserControl_Loaded

DoubleAnimation _Animation; 
    private Storyboard _StoryBoard; 

    private void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     Canvas.SetZIndex(Image1, 1); 

     _Animation = new DoubleAnimation(); 
     _Animation.From = -Image1.ActualHeight; 
     _Animation.To = cnvMain.ActualHeight; 
     _Animation.RepeatBehavior = RepeatBehavior.Forever; 
     _Animation.Duration = new Duration(TimeSpan.Parse("0:0:10")); 
     _Animation.FillBehavior = FillBehavior.Stop; 
     Storyboard.SetTarget(_Animation, Image1); 
     Storyboard.SetTargetProperty(_Animation, new PropertyPath(Canvas.BottomProperty)); 
     _StoryBoard = new Storyboard(); 
     _StoryBoard.Children.Add(_Animation); 
     _StoryBoard.Begin(); 
     _StoryBoard.Pause(); 
    } 
    public void Pause() 
    { 
     _StoryBoard.Pause(); 
    } 
    public void Up() 
    { 
     _Animation.From = cnvMain.ActualHeight; 
     _Animation.To = -Image1.ActualHeight; 
     _StoryBoard.Resume(); 

    } 
    public void Down() 
    { 
     _Animation.From = Image1.ActualHeight; 
     _Animation.To = -cnvMain.ActualHeight; 
     _StoryBoard.Resume(); 

    } 

動畫初始化從和到。我的問題是這種屬性並沒有改變在Down和Top方法調用(方法工作,但圖像向上移動)

+0

您的方法是Down和Up沒有Down和Top。這兩種方法中的代碼是相同的。 – Paparazzi

回答

0

我改變它喜歡下面,它的工作。

DoubleAnimation _Animation; 
    private Storyboard _StoryBoard; 

    private void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     LoadAnimaiton(false); 
     _StoryBoard.Pause(); 
    } 

    private void LoadAnimaiton(bool up) 
    { 
     _StoryBoard = new Storyboard(); 
     _Animation = new DoubleAnimation(100.0, new Duration(TimeSpan.Parse("0:0:10"))); 
     _Animation.From = cnvMain.ActualHeight; 
     _Animation.To = -Image1.ActualHeight; 
     _Animation.RepeatBehavior = RepeatBehavior.Forever; 
     _Animation.FillBehavior = FillBehavior.Stop; 
     Storyboard.SetTarget(_Animation, Image1); 
     Storyboard.SetTargetProperty(_Animation, new PropertyPath(Canvas.BottomProperty)); 

     if (up) 
     { 
      _Animation.From = cnvMain.ActualHeight; 
      _Animation.To = -cnvMain.ActualHeight; 
     } 
     else 
     { 
      _Animation.From = -cnvMain.ActualHeight; 
      _Animation.To = cnvMain.ActualHeight; 
     } 

     _StoryBoard.Children.Add(_Animation); 

     _StoryBoard.Begin(); 
    } 
    public void Pause() 
    { 
     _StoryBoard.Pause(); 
    } 
    public void Down() 
    { 
     LoadAnimaiton(true); 
    } 

    public void Up() 
    { 
     LoadAnimaiton(false); 
    }