gpt4 book ai didi

c# - WPF:无法在样式中的目标标记上指定事件 'Completed'。改用 EventSetter

转载 作者:太空狗 更新时间:2023-10-29 21:37:59 26 4
gpt4 key购买 nike

我正在尝试在 Storyboard完成后运行一些代码,但似乎找不到如何设置它...如果我只是这样做

Completed="LoadingStoryBoard_Completed"

在 Storyboard 元素上,我收到错误消息“无法在样式中的目标标记上指定事件‘Completed’。请改用 EventSetter。”但是很难找到有关如何使用事件 setter 的良好引用。

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>

非常感谢所有提示

最佳答案

Style 中取出你的 Storyboard 并独立定义它。

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

关于c# - WPF:无法在样式中的目标标记上指定事件 'Completed'。改用 EventSetter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com