2012-11-18 116 views
2

我有以下XAML網格:XAML故事板的背景圖像刷

<Grid Style="{StaticResource LayoutRootStyle}" x:Name="mainGrid"> 
    <Grid.Resources> 
     <Storyboard x:Name="FadeOut"> 
      <DoubleAnimation Duration="3" To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="gridBackgroundImageBrush" d:IsOptimized="True"/> 
     </Storyboard> 
     <Storyboard x:Name="FadeIn"> 
      <DoubleAnimation Duration="3" To="0.35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="gridBackgroundImageBrush" d:IsOptimized="True"/> 
     </Storyboard> 
    </Grid.Resources> 

    <Grid.Background> 
     <ImageBrush x:Name="gridBackgroundImageBrush" ImageSource="{Binding BackgroundImage}" Opacity="0.35"> 
     </ImageBrush> 
    </Grid.Background> 

我想以編程方式啓動的「淡出」的動畫,改變從圖像刷的圖像,然後啓動「淡入」的動畫,就像這樣:

private void t_Tick(object sender, object e) 
    { 
     try 
     { 
      FadeOut.Begin(); 
      this.DefaultViewModel["BackgroundImage"] = BackgroundImage; 
      FadeIn.Begin(); 
     } 
     catch { } 
    } 

但是圖像在沒有任何動畫的情況下改變。我想這個問題是關於如何訪問ImageBrush的「不透明度」屬性。我嘗試了 TargetProperty屬性的語法如下:

(Control.Background).(ImageBrush.Opacity) 

爲MSDN所示的是:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.animation.storyboard.settargetproperty.aspx,但它似乎並沒有工作。有人可以幫我解決這個問題嗎?

回答

2

的解決方案是創建一個圖像控制,而不是與繪製圖像刷圖像,然後限定視覺狀態衰落:

<Grid Style="{StaticResource LayoutRootStyle}" x:Name="mainGrid"> 
    <Image Grid.RowSpan="2" x:Name="gridBackgroundImageBrush" Source="{Binding BackgroundImage}" /> 
</Grid> 
<VisualStateGroup x:Name="FadeStates"> 
     <VisualState x:Name="FadeOutState"> 
      <Storyboard> 
        <DoubleAnimation Duration="{Binding fadeDuration}" From="0.5" To="0.0" x:Name="fadeOutAnimation" 
            Storyboard.TargetProperty="Opacity" 
            Storyboard.TargetName="gridBackgroundImageBrush" /> 
      </Storyboard> 
     </VisualState> 
     <VisualState x:Name="FadeInState"> 
      <Storyboard> 
        <DoubleAnimation Duration="{Binding fadeDuration}" From="0.0" To="0.5" x:Name="fadeInAnimation" 
            Storyboard.TargetProperty="Opacity" 
            Storyboard.TargetName="gridBackgroundImageBrush" /> 
      </Storyboard> 
     </VisualState> 
</VisualStateGroup>