0
我想了解一些關於Storyboard和動畫的內容,所以我決定做一個簡單的閃爍按鈕。在Windows Phone上做閃爍/閃爍按鈕
經過一番努力,我已經能夠錄製動畫,但它不能按預期工作。
動畫播放一次,它似乎忽略我的SpeedRatio
屬性。 我甚至已經失去了Pressed
動畫!
這裏是我的代碼:
XAML
<UserControl x:Class="BPMFlashingButton.BPMFlashingButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<UserControl.Resources>
<Style x:Key="BPMFlashingButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
</VisualStateGroup>
<VisualStateGroup x:Name="CustomStates">
<VisualState x:Name="Flashing">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background)" Storyboard.TargetName="LayoutRoot" RepeatBehavior="Forever">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{StaticResource PhoneBackgroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Button x:Name="FlashingButton" Content="Blink" Style="{StaticResource BPMFlashingButton}" BorderBrush="{StaticResource PhoneBorderBrush}"/>
然後我有了這個方法
public void startAnimation()
{
VisualStateManager.GoToState(FlashingButton, "Flashing", true);
}
可以使動畫開始。
但是,當我打電話給它,按鈕變成紅色,並保持不動,不閃爍!
有什麼建議嗎?
我無法測試自己的atm,但可以嘗試向ObjectAnimationUsingKeyFrames添加AutoReverse =「True」。 – robertk
剛剛嘗試過,但它的工作原理和以前一樣:( – StepTNT
你試過在CustomStates VSM組中添加另一個空狀態嗎?我非常確定VSM在同一組中的兩個或多個狀態之間進行動畫處理,我不確定它如何「我只有一個狀態 – JustinAngel