我正在尝试WPF并在为我的“Othello Buttons”创建动画时遇到问题。
所以,我用一些属性创建了一个新的类Token : Button
。我还添加了RessourceDictionary
和StoryBoard
。现在我想在Switch Case中启动它。当我这样做时,我收到以下错误:
没有适用的名称范围来解析名称'ellipse'。
<!-- TOKEN TEMPLATE -->
<Style x:Key="Token" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="ToWhite">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ellipse">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="ellipse">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="Black"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="ellipse">
<EasingColorKeyFrame KeyTime="0" Value="Black"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="ToBlack">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ellipse">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="ToEnabled">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="ellipse">
<EasingColorKeyFrame KeyTime="0" Value="Black"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="White"/>
</ColorAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ellipse">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.19"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
这是我的令牌类
public class Token : Button
{
Storyboard sbToWhite = new Storyboard();
Storyboard sbToBlack = new Storyboard();
Storyboard sbToEnabled = new Storyboard();
public Token()
{
BorderBrush = new SolidColorBrush(Colors.Gainsboro);
sbToBlack = (Storyboard)TryFindResource("ToBlack");
sbToEnabled = (Storyboard)TryFindResource("ToEnabled");;
}
private GameState.TokenState _status;
public Point Point { get; set; }
public GameState.TokenState Status { get { return _status; }
set
{
_status = value;
switch (value)
{
case GameState.TokenState.NONE:
IsSet = false;
break;
case GameState.TokenState.WHITE:
((Storyboard)TryFindResource("ToWhite")).Begin();
IsSet = true;
break;
case GameState.TokenState.BLACK:
sbToBlack.Begin();
IsSet = true;
break;
case GameState.TokenState.ENABLED:
sbToEnabled.Begin();
IsSet = false;
break;
}
}