我正在尝试 WPF 并在为我的“奥赛罗按钮”创建动画时遇到问题。
所以,我创建了一个Token : Button
带有一些属性的新类。而且我还添加了RessourceDictionary
一个StoryBoard
。现在我想在 Switch Case 中启动它。当我这样做时,我收到以下错误:
不存在可解析名称“椭圆”的适用名称范围。
<!-- 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;
}
}