我有一个动画的问题。我有一个UserControl,显示一个标题和一个箭头图像,它应该在点击时旋转90°。但我得到一个InvalidOperationException“无法解析xx TargetName。”
<Image
x:Name="ImageArrow"
HorizontalAlignment="Right"
Margin="0,0,20,0"
VerticalAlignment="Center"
Source="/Images/konto_pfeil.png"
Height="24"
Width="15"
RenderTransformOrigin="0.5,0.5">
<i:Interaction.Behaviors>
<local:AnimateOnDataBehavior Value="{Binding Dropped, ElementName=BaseView}">
<local:AnimateOnDataBehavior.StoryboardTrue>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ImageArrow"
Storyboard.TargetProperty="(FrameworkElemet.RenderTransform).(RotateTransform.Angle)"
To="90"
Duration="0:0:0.3">
<DoubleAnimation.EasingFunction>
<CubicEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</local:AnimateOnDataBehavior.StoryboardTrue>
<local:AnimateOnDataBehavior.StoryboardFalse>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ImageArrow"
Storyboard.TargetProperty="(FrameworkElemet.RenderTransform).(RotateTransform.Angle)"
To="0"
Duration="0:0:0.3">
<DoubleAnimation.EasingFunction>
<CubicEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</local:AnimateOnDataBehavior.StoryboardFalse>
</local:AnimateOnDataBehavior>
</i:Interaction.Behaviors>
<Image.RenderTransform>
<RotateTransform Angle="0"/>
</Image.RenderTransform>
</Image>
我在同一个项目中成功使用AnimateOnDataBehavior,所以我认为不是问题,但无论如何你可以快速看一下:
public class AnimateOnDataBehavior : Behavior<FrameworkElement>
{
/// <summary>
/// Invert the value conversion logic
/// </summary>
public bool Invert
{
get { return (bool)GetValue(InvertProperty); }
set { SetValue(InvertProperty, value); }
}
// Using a DependencyProperty as the backing store for Invert. This enables animation, styling, binding, etc...
public static readonly DependencyProperty InvertProperty =
DependencyProperty.Register("Invert", typeof(bool), typeof(AnimateOnDataBehavior), new PropertyMetadata(false));
/// <summary>
/// Value, object != null, string !string.IsNullOrEmpty(), bool == true, conversion logic.
/// </summary>
public object Value
{
get { return (object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(AnimateOnDataBehavior), new PropertyMetadata(new PropertyChangedCallback(ValueChanged)));
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as AnimateOnDataBehavior).OnValueChanged();
}
public Storyboard StoryboardTrue
{
get { return (Storyboard)GetValue(StoryboardTrueProperty); }
set { SetValue(StoryboardTrueProperty, value); }
}
// Using a DependencyProperty as the backing store for StoryboardTrue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StoryboardTrueProperty =
DependencyProperty.Register("StoryboardTrue", typeof(Storyboard), typeof(AnimateOnDataBehavior), new PropertyMetadata(null));
public Storyboard StoryboardFalse
{
get { return (Storyboard)GetValue(StoryboardFalseProperty); }
set { SetValue(StoryboardFalseProperty, value); }
}
// Using a DependencyProperty as the backing store for StoryboardFalse. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StoryboardFalseProperty =
DependencyProperty.Register("StoryboardFalse", typeof(Storyboard), typeof(AnimateOnDataBehavior), new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += (_, __) =>
{
Animate();
};
}
private bool ConversionLogic()
{
object v = Value;
bool b = false;
if (v is string)
{
b = string.IsNullOrEmpty((string)v);
}
else if (v is bool)
{
b = (bool)v;
}
else
{
b = v != null;
}
return Invert ? !b : b;
}
private bool? lastFade = null;
private void Animate()
{
bool bFadeIn = ConversionLogic();
// Just stop senseless animations
if (lastFade != null && lastFade == bFadeIn)
return;
lastFade = bFadeIn;
Storyboard storyboard = bFadeIn ? StoryboardTrue : StoryboardFalse;
if (storyboard != null)
{
try { storyboard.Begin(); }
catch { }
}
}
private void OnValueChanged()
{
Animate();
}
}