WPF:活动'已完成'无法在Style中的Target标记上指定。请改用EventSetter

时间:2017-02-28 13:39:41

标签: c# wpf xaml

我试图在故事板完成时运行一些代码,但似乎无法找到如何设置它...如果我只是做

Completed="LoadingStoryBoard_Completed"

在Storyboard元素上,我收到错误"事件'已完成'无法在Style中的Target标记上指定。请改用EventSetter。"但很难找到关于如何使用事件设定器的良好参考。

Xaml代码看起来像这样(它的结构如下,以便在图像可见时启动)

<Window.Resources>
    <Style x:Key="AnimationImageStyle" TargetType="{x:Type Image}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsVisible}" 
             Value="True">

                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard Name="LoadingStoryBoard"  >

                            <!-- -->
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
                                               Duration="0:0:4">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2a-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:1">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2b-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:2">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2c-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:3">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2d-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Grid Name="GridLoading" Visibility="Hidden" >

        <Image Style="{StaticResource AnimationImageStyle}" >
        </Image>

</Grid>

所有提示非常感谢

2 个答案:

答案 0 :(得分:3)

Storyboard获取Style并将其定义为独立。

<Storyboard x:Key="SbImgKey">

            <!-- -->
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
                                               Duration="0:0:4" Completed="ObjectAnimationUsingKeyFrames_Completed_1">
                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                    <DiscreteObjectKeyFrame.Value>
                        <BitmapImage UriSource="C:\Users\Anjum\Pictures\copy\koala.jpg"/>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
                <DiscreteObjectKeyFrame KeyTime="0:0:1">
                    <DiscreteObjectKeyFrame.Value>
                        <BitmapImage UriSource="C:\Users\Anjum\Pictures\copy\desert.jpg"/>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
                <DiscreteObjectKeyFrame KeyTime="0:0:2">
                    <DiscreteObjectKeyFrame.Value>
                        <BitmapImage UriSource="C:\Users\Anjum\Pictures\copy\tulips.jpg"/>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>

立即使用

<Style x:Key="AnimationImageStyle" TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsVisible}" 
             Value="True">

                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource SbImgKey}"/>                          
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>

答案 1 :(得分:1)

我不确定,但你可以试试这个:

<Grid Name="GridLoading" Visibility="Hidden" >
        <Image Name="MyImage">
        <Image.Resources> 
         <Storyboard Name="LoadingStoryBoard" 
                    Completed="LoadingStoryBoard_Completed">
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
                                               Duration="0:0:4">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2a-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:1">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2b-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:2">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2c-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:3">
                                    <DiscreteObjectKeyFrame.Value>
                                        <BitmapImage UriSource="C:\...\2d-loading.jpg"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
        </Image.Resources>
         <Image.Style>
        <Style>
            <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsVisible}" 
                         Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="LoadingStoryBoard"/>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
        </Style>
      </Image.Style>
    </Image>
</Grid>