WPF:无法在样式的目标标签上指定事件“已完成”。改用EventSetter

Tim*_*ens 6 c# wpf xaml

我正在尝试在故事板完成后运行一些代码,但似乎找不到如何设置的代码...

Completed="LoadingStoryBoard_Completed"
Run Code Online (Sandbox Code Playgroud)

在情节提要元素上,我收到错误消息“无法在样式的目标标签上指定事件'已完成'。请改用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>
Run Code Online (Sandbox Code Playgroud)

所有提示大加赞赏

Anj*_*han 8

你滚出去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>
Run Code Online (Sandbox Code Playgroud)

立即使用

<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>
Run Code Online (Sandbox Code Playgroud)