用于背景ImageBrush的XAML故事板

时间:2012-11-18 19:41:34

标签: xaml windows-8 windows-store-apps

我有以下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>

我想以编程方式启动“FadeOut”动画并从ImageBrush更改Image,然后启动“FadeIn”动画,如下所示:

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

然而,图像在没有任何动画的情况下发生变化。我想问题是关于我如何访问ImageBrush的“Opacity”属性。我尝试了以下语法 TargetProperty属性:

(Control.Background).(ImageBrush.Opacity)

正如msdn在此处所示:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.animation.storyboard.settargetproperty.aspx但它似乎不起作用。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

解决方案是创建一个图像控件,而不是使用ImageBrush绘制图像,然后定义淡入淡出的视觉状态:

<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>