我有一个使用 EventTriggers 启动/停止动画的控件。除了第一次初始化控件时,一切正常,EventTriggers 没有被触发。我怀疑当属性更改值时它们尚未设置,因此不会触发和启动动画。
如果我在运行时更改IsBusy
属性值,一切正常。
我究竟做错了什么?
导致问题的用法(StartEvent 被触发但 EventTrigger 似乎没有捕捉到它):
<my:BusyIndicator IsBusy="True" x:Name="testIndicator" Height="100" Width="100"/>
我控制的相关代码:
/// <summary>
/// Event that starts the animation
/// </summary>
public static readonly RoutedEvent StartEvent;
/// <summary>
/// Event that stops the animation
/// </summary>
public static readonly RoutedEvent StopEvent;
static BusyIndicator()
{
DefaultStyleKeyProperty.
OverrideMetadata(
typeof(BusyIndicator),
new FrameworkPropertyMetadata(typeof(BusyIndicator))
);
StartEvent =
EventManager.RegisterRoutedEvent(
"StartEvent",
RoutingStrategy.Direct,
typeof(RoutedEventHandler),
typeof(BusyIndicator));
StopEvent =
EventManager.RegisterRoutedEvent(
"StopEvent",
RoutingStrategy.Direct,
typeof(RoutedEventHandler),
typeof(BusyIndicator));
}
/// <summary>
/// Indicates if the control is currently animated.
///
/// This is a dependency property
/// </summary>
public bool IsBusy
{
get { return (bool)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}
/// <summary>
/// Identifier for the IsBusy property
/// </summary>
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register(
"IsBusy",
typeof(bool),
typeof(BusyIndicator),
new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsBusyChanged)));
private static void OnIsBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs inEventArgs)
{
bool newValue = (bool)inEventArgs.NewValue;
if (newValue)
{
System.Diagnostics.Debug.WriteLine("Start");
(sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StartEvent));
}
else
{
System.Diagnostics.Debug.WriteLine("Stop");
(sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StopEvent));
}
}
我的 Themes\generic.xaml 文件中的相关 XAML
<Storyboard x:Key="rotateAnimation">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="pathCircular"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"
RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Style TargetType="{x:Type local:BusyIndicator}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BusyIndicator}">
<Grid Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Margin="{TemplateBinding Margin}">
<Path Stretch="Fill" Name="pathCircular"
<!-- the path being animated -->
</Path>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="local:BusyIndicator.StopEvent">
<StopStoryboard BeginStoryboardName="rotateAnimation"/>
</EventTrigger>
<EventTrigger RoutedEvent="local:BusyIndicator.StartEvent">
<BeginStoryboard Name="rotateAnimation" Storyboard="{StaticResource rotateAnimation}"/>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>