2013-05-05 47 views
4

我正在开发Windows 8音乐应用程序。我正在改变当前歌曲/相册的图像背景。我想在我改变图像的同时添加fadeIn/dafeOut动画,但无法弄清楚我可以如何做到这一点。更改背景图片时的动画(C#winrt)

<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设置为“不透明度”,则动画可以运行,但适用于整个页面而不仅仅是背景。

回答

3

不要将图像作为页面的背景,添加另一个将保留背景的网格/边框。在动画中使用Opacity作为TargetProperty

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