在WPF中旋转可见的元素集

时间:2015-08-10 14:44:36

标签: c# wpf animation wpf-animation

这更多是针对潜在客户的广告交易,因此没有人工互动组件。

现在我只有绑定到ItemsControl的元素和一个故事板动画循环。不幸的是,我希望一次显示4个项目,暂停10秒,然后显示下一个4.我可以有5个优惠券,我可以有30个,所以我不能静态输入任何内容,除非我知道我的可见宽度(它们将水平旋转)是1920px。

我当前的实现,显示1到4然后是5到8并循环回" 1"是:

        <ItemsControl Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding Path=VisibleDigitalCoupons}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Storyboard x:Key="RotateDigitalCoupons" BeginTime="0:0:0" Duration="0:0:10" RepeatBehavior="Forever" Completed="RotateDigitalCouponsCompleted">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" Storyboard.TargetName="digitalCouponView">
                                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"></EasingDoubleKeyFrame>
                                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="-1920">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CubicEase EasingMode="EaseInOut"></CubicEase>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </DataTemplate.Resources>
                    <views:DigitalCouponView x:Name="digitalCouponView" Margin="40,40,20,20" Height="240" Width="420" RenderTransformOrigin="0.5,0.5" >
                        <views:DigitalCouponView.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform/>
                            </TransformGroup>
                        </views:DigitalCouponView.RenderTransform>
                    </views:DigitalCouponView>
                    <DataTemplate.Triggers>
                        <EventTrigger RoutedEvent="Window.Loaded">
                            <BeginStoryboard x:Name="RotateDigitalCoupons_BeginStoryboard" Storyboard="{StaticResource RotateDigitalCoupons}"/>
                        </EventTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

尝试使用已完成的事件触发刷新&#34;可见的优惠券&#34;并且重用动画遇到了失败,因为动画永远在重复行为上。然而,即使有了这个,我也没有完成事件的发射,所以这是一个死胡同AFAIK。

以前有人有任何想法或处理过吗?我的流程是否存在缺陷?

1 个答案:

答案 0 :(得分:1)

创建一个Dispatch Timer,它包含一个状态机,它将根据当前状态执行逻辑并处理将显示的数据驱动组件。在计时器内根据需要打开和关闭动画。

当然,您需要使动画更通用,但您可以使用定时器可以利用的框架。