1

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>
4

1 に答える 1

1

テンプレートが適用されていない場合、イベントをリッスンすることはできません。OnApplyTemplateの後にフラグを保持してイベントを発生させるか、IsBusyプロパティへのトリガーを使用してEnterActionsExitActionsを使用する必要があります。

于 2012-10-16T00:52:19.527 に答える