2

我有以下 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 更改图像,然后启动“FadeIn”动画,如下所示:

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但它似乎不起作用。有人可以帮我解决这个问题吗?

4

1 回答 1

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>
于 2012-11-19T14:27:05.217 回答