XAML Storyboard for Background ImageBrush

寵の児 提交于 2019-12-11 02:33:26

问题


I have the following XAML Grid:

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

I want to programmatically start the "FadeOut" animation and change the Image from ImageBrush, then start the "FadeIn" animation, like this:

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

However the image is changing without any animation. I guess the problem is about how I'm accessing the "Opacity" property of the ImageBrush. I tried the following syntax for the TargetProperty attribute:

(Control.Background).(ImageBrush.Opacity)

as msdn shows here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.animation.storyboard.settargetproperty.aspx but it doesn't seem to work. Can someone help me with this problem?


回答1:


The solution was to create an image control rather than drawing the image with ImageBrush and then defining visual states for fading:

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


来源:https://stackoverflow.com/questions/13443854/xaml-storyboard-for-background-imagebrush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!