4

我正在开发 Windows 8 音乐应用程序。我正在用当前歌曲/专辑的图像更改页面背景。我想在更改图像时添加 fadeIn/dafeOut 动画,但不知道该怎么做。

    <Grid  x:Name="LayoutRoot" Background="{StaticResource  ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <Storyboard x:Name="fadeOutStoryBoard">
                <DoubleAnimation
                    Storyboard.TargetName="LayoutRoot"
                    Storyboard.TargetProperty="(LayoutRoot.Background).(ImageBrush.Opacity)"
                    From="1.0" To="0.0" Duration="0:0:10"/>
            </Storyboard>
            <Storyboard x:Name="fadeInStoryBoard">
                <DoubleAnimation
                    Storyboard.TargetName="LayoutRoot"
                    Storyboard.TargetProperty="(LayoutRoot.Background).(ImageBrush.Opacity)"
                    From="0" To="1.0" Duration="0:0:10"/>
            </Storyboard>
        </Grid.Resources>
    </Grid>

    In C# code: 
    ImageBrush image = new ImageBrush();
    image.ImageSource = new BitmapImage(imageUri);
    fadeOutStoryBoard.Begin();
    MainPage.Current.LayoutRoot.Background = image;
    fadeInStoryBoard.Begin();

图像变化很好,但我没有看到动画。我尝试将 TargetProperty 更改为 (LayoutRoot.Background).Opacity 或 (LayoutRoot.Background).(SolidColorBrush.Opacity) 但没有运气。如果我将 TargetProperty 设置为“不透明度”,则动画可以工作,但适用于整个页面而不仅仅是背景。

4

1 回答 1

3

不要将图像作为页面背景,添加另一个网格/边框来保存背景。OpacityTargetProperty动画中使用。

原因是当前动画正在使用旧的图像画笔,而不是您创建的新画笔。

于 2013-05-05T12:13:43.560 回答