WPF VisualTransition很慢

时间:2018-06-15 09:58:38

标签: c# wpf

所以我在Microsoft page上尝试这个例子。 我遇到的问题是过渡时间约为2-3秒。 当我设置GeneratedDuration时,我可以让它变慢但速度不快。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Test_App" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">


<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#888888" Offset="0"/>
    <GradientStop Color="#555555" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="MouseHover" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#AAAAAA" Offset="0"/>
    <GradientStop Color="#888888" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#121212"/>
<Style TargetType="{x:Type Button}">


    <Setter Property="Background" Value="#444444"/>
    <Setter Property="BorderBrush" Value="#444444"/>
    <Setter Property="Foreground" Value="#BBBBBB"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="RootElement">

                    <VisualStateManager.VisualStateGroups>

                        <VisualStateGroup x:Name="CommonStates">

                            <VisualState x:Name="Normal" />

                            <VisualState x:Name="MouseOver" >

                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush" 
                                                    Storyboard.TargetProperty="Color" 

                                                    To="Red" />
                                </Storyboard>
                            </VisualState>

                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush" 
                                                    Storyboard.TargetProperty="Color"
                                                    To="Transparent"/>              
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Border.Background>
                        <SolidColorBrush x:Name="BorderBrush" Color="#999999"/>
                    </Border.Background>
                    <Border.BorderThickness>  1   </Border.BorderThickness>
                    <Grid Background="{TemplateBinding Background}" Margin="1">
                        <ContentPresenter   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                                
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                                                 
                                            />
                    </Grid>
                </Border>
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>

我不清楚它为何如此缓慢。正如我所说的那样,我尝试使用GeneratedDuration似乎可能会更快一点,但无论我提出多快,它仍然需要至少2秒。

1 个答案:

答案 0 :(得分:1)

ColorAnimation(以及从Timeline派生的所有其他动画)都有一个名为Duration的属性。其默认值为Automatic,这似乎会导致持续时间约为2-3秒。

如果明确设置持续时间,则可以缩短或延长持续时间。在以下示例中,持续时间为0.5秒:

<VisualState x:Name="MouseOver" >
    <Storyboard>
        <ColorAnimation Storyboard.TargetName="BorderBrush" 
                Storyboard.TargetProperty="Color" 
                To="Red"
                Duration="0:0:0.5"/>
    </Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
    <Storyboard>
        <ColorAnimation Storyboard.TargetName="BorderBrush" 
                Storyboard.TargetProperty="Color"
                To="Transparent"
                Duration="0:0:0.5"/>
    </Storyboard>
</VisualState>