有没有办法在metro风格的应用程序中设置Margin属性的动画

时间:2012-05-08 13:14:47

标签: xaml animation storyboard windows-8 windows-runtime

我正在尝试使用Storyboard类从代码中做一些动画。没有ThicknessAnimation类。我还尝试使用Blend构建故事板,它在那里不起作用。它只是直接跳到新值,没有平滑的动画。

更新:我尝试使用TranslateX转换。但是当我在图像上使用它时,图像会被剪裁。 我想要做的是在一个小网格中设置一个非常慢的大图像,所以它有这种效果(类似于Zune和Windows Phone Gallery中的一个)。图像打开后,我开始动画,这是我的代码:

private void Image_ImageOpened_1(object sender, RoutedEventArgs e)
    {
        var img = sender as Image;

        Storyboard sb = new Storyboard();
        var doubleAnimationx = new DoubleAnimation() { To = -100, SpeedRatio = 0.1, From = 0 };
        Storyboard.SetTarget(doubleAnimationx, img);
        Storyboard.SetTargetProperty(doubleAnimationx, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)");
        sb.Children.Add(doubleAnimationx);
        sb.Begin();
    }

的Xaml:

<Grid  IsSwipeEnabled="True" ItemsSource="{Binding Source={StaticResource cvs1}}" ItemClick="ItemsGridView_ItemClick_1"
                            x:Name="ItemsGridView" Margin="50,20,116,46" SelectionMode="None" IsItemClickEnabled="True"
                            AutomationProperties.AutomationId="ItemsGridView" 
                            AutomationProperties.Name="Grouped Items">
                <Grid.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="250" VariableSizedWrapGrid.ColumnSpan="{Binding ColumnSpan}" Margin="2">
                            <Image ImageOpened="Image_ImageOpened_1" Stretch="UniformToFill" Source="{Binding ImageHQ}" >
                                <Image.RenderTransform>
                                    <CompositeTransform />
                                </Image.RenderTransform>
                            </Image>
                            <StackPanel VerticalAlignment="Bottom" Background="#AA000000">
                                <TextBlock Margin="5,5,5,0" FontSize="26" Text="{Binding Name,Mode=OneWay}" FontFamily="Arial Black" />
                                <TextBlock Margin="5,0,5,5" FontSize="24" Text="{Binding Section,Mode=OneWay}" Foreground="{Binding SectionColor,Mode=OneWay}" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </Grid.ItemTemplate>
</Grid>

2 个答案:

答案 0 :(得分:20)

首先,设置边距动画不是一个好主意(它需要更新整个树)。你想达到什么样的效果?你想移动物体吗?如果是,请使用DoubleAnimation更改TranslateTransform。

我在Windows 8中没有这样做,但我敢打赌与WPF几乎相同。最好在XAML中定义动画

<Window.Resources>

    <Storyboard  x:Key="mainInAnimation">
        <DoubleAnimation Storyboard.TargetName="panelTrans" 
                                Storyboard.TargetProperty="X"
                                BeginTime="0:0:0.2"
                                Duration="0:0:0.3" To="0" >
            <DoubleAnimation.EasingFunction>
                <ExponentialEase  EasingMode="EaseOut"  />
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>

然后你需要渲染变换到面板

    <StackPanel Name="leftPanel"  ... >
        <StackPanel.RenderTransform>
            <TranslateTransform x:Name="panelTrans" X="-10"></TranslateTransform>
        </StackPanel.RenderTransform>

在代码中开始动画(我更喜欢这种方式)

        Storyboard anim = (Storyboard)this.Resources["mainInAnimation"];
        anim.Begin();

答案 1 :(得分:7)

我今天遇到了同样的问题。为了解决这个问题,我做了以下几点:

我的故事板首先将边距重置为我想要的边距,并通过将TranslateTransform设置为oposite来对抗它。这具有无效的效果,但现在我可以在TranslateTransform中实现我的动画并且图像显示。

<Storyboard x:Name="rotateViews">
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="root">
        <DiscreteObjectKeyFrame KeyTime="0">
            <DiscreteObjectKeyFrame.Value>
                <Thickness>0,-100,0,0</Thickness>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="root">
        <EasingDoubleKeyFrame KeyTime="0" Value="100"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0">
            <EasingDoubleKeyFrame.EasingFunction>
                <CubicEase EasingMode="EaseInOut"/>
            </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
相关问题