2011-05-02 197 views
0

我試圖讓一些圖像循環,有點像你在老虎機中看到的。 我目前的代碼更多的是一個選取框,所以圖像從頂部到底部,而他們正在移動一些空白後面的第一張圖片。WPF連續動畫

我該如何讓最後一幅圖像在循環中跟隨第一幅圖像?

XAML:

<Window x:Class="SlotV3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Slot Machine" Height="600" Width="800" Loaded="Window_Loaded"> 
<Grid> 
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="634,185,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
    <Canvas Name="canvas1" Margin="82,49,458,12" ClipToBounds="True" Height="500"> 
     <StackPanel Height="1043" Name="stackPanel1" Width="193" Canvas.Left="28" Canvas.Top="8"> 
      images here... 
     </StackPanel> 
    </Canvas> 
</Grid> 

,並使用動畫代碼IM:

DoubleAnimation da = new DoubleAnimation(); 
     da.From = -stackPanel1.ActualHeight; 
     da.To = canvas1.ActualHeight; 
     da.Duration = new Duration(TimeSpan.FromSeconds(0.25)); 
     da.RepeatBehavior = RepeatBehavior.Forever; 
     stackPanel1.BeginAnimation(Canvas.TopProperty, da); 

回答

0

我想通過以下方式更容易。嘗試使用此事件:

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); 
    } 

    void CompositionTarget_Rendering(object sender, EventArgs e) 
    { 
     Rectangle smoke = new Rectangle(); 
     LayoutRoot.Children.Add(smoke); 
     smoke.Y -= 5; 
    } 
} 

這是一個僞代碼,但此策略將使對象從底部運行到頂部。

http://msdn.microsoft.com/en-us/library/system.windows.media.compositiontarget.rendering.aspx#Y228