在动画需要之前加载ImageBrush staticresources?

时间:2014-06-25 16:22:45

标签: c# xaml windows-runtime winrt-xaml

我有一个按钮控件,我已经设置了一个模板,以便它将循环通过一堆图像作为其正常动画。我的问题是,它第一次循环闪烁和口吃(我相信图像正在加载)。有没有办法让它们全部预加载?

<Application.Resources>
    <ImageBrush x:Key="First" ImageSource="Assets/btn1.png" />
    <ImageBrush x:Key="Second" ImageSource="Assets/bt2.png" />
    <ImageBrush x:Key="Third" ImageSource="Assets/bt3.png" />
    <ImageBrush x:Key="Fourth" ImageSource="Assets/bt4.png" />

    <Thickness x:Key="ButtonBorderThemeThickness">2</Thickness>
    <Style  TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard RepeatBehavior="Forever">
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource First}" ></DiscreteObjectKeyFrame>
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{StaticResource Second}" ></DiscreteObjectKeyFrame>
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{StaticResource Third}" ></DiscreteObjectKeyFrame>
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{StaticResource Fourth}" ></DiscreteObjectKeyFrame>

                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border"  >

                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

1 个答案:

答案 0 :(得分:1)

试试这个

<Application.Resources>
    <ImageBrush x:Key="First" ImageSource="Assets/btn1.jpg" />
    <ImageBrush x:Key="Second" ImageSource="Assets/btn2.png" />
    <ImageBrush x:Key="Third" ImageSource="Assets/btn3.png" />
    <ImageBrush x:Key="Fourth" ImageSource="Assets/btn4.png" />
    <Style x:Key="buttonStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border" >
                            <Border.Triggers>
                                <EventTrigger>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames RepeatBehavior="Forever"  Duration="0:0:0.4" Storyboard.TargetName="Border"  Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{StaticResource First}" ></DiscreteObjectKeyFrame>
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{StaticResource Second}" ></DiscreteObjectKeyFrame>
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{StaticResource Third}" ></DiscreteObjectKeyFrame>
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.4" Value="{StaticResource Fourth}" ></DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Border.Triggers>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

<Button Height="100" Width="100" Style="{StaticResource buttonStyle}"/>