前提
C# (WPF)に関する質問です。
ResourceDictionaryに定義したStoryboardのComletedイベントを受け取って処理をしたく、以下のようなコードを組みました。
しかしイベントが発生せず、原因がわからないため質問しました。
該当のソースコード
MainWindow.xaml.cs
1namespace Sample 2{ 3 /// <summary> 4 /// Interaction logic for MainWindow.xaml 5 /// </summary> 6 public partial class MainWindow : Window 7 { 8 public MainWindow() 9 { 10 InitializeComponent(); 11 } 12 13 private void Page_Loaded(object sender, RoutedEventArgs e) 14 { 15 var sb = FindResource("SampleStoryboard") as Storyboard; 16 sb.Completed += Sb_Completed; 17 BeginStoryboard(sb); 18 } 19 20 private void Sb_Completed(object sender, EventArgs e) 21 { 22 Debug.WriteLine("イベント発生"); 23 } 24 25 } 26}
MainWindow.xaml
1<Window x:Class="Sample.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:gif="http://wpfanimatedgif.codeplex.com" 7 xmlns:local="clr-namespace:Sample" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="768" Width="1024"> 10 11 <Window.Background> 12 <ImageBrush ImageSource="/resources/bgimage/bgImage1.jpg" /> 13 </Window.Background> 14 <Window.Resources> 15 <ResourceDictionary> 16 <ResourceDictionary.MergedDictionaries> 17 <ResourceDictionary Source="AnimationDictionary.xaml"/> 18 </ResourceDictionary.MergedDictionaries> 19 </ResourceDictionary> 20 </Window.Resources> 21 <Window.Resources> 22 <ResourceDictionary> 23 <ResourceDictionary.MergedDictionaries> 24 <ResourceDictionary Source="SampleDictionary.xaml"/> 25 </ResourceDictionary.MergedDictionaries> 26 </ResourceDictionary> 27 </Window.Resources> 28 29 <Canvas> 30 <MediaElement x:Name = "media"/> 31 <Canvas x:Name="ObjectLayer"> 32 <Image x:Name="Object" 33 Source="object.png" 34 Height="349" Width="301" 35 Canvas.Left="374" Canvas.Top="284" 36 RenderTransformOrigin="0.5,0.5"> 37 <Image.RenderTransform> 38 <TransformGroup> 39 <ScaleTransform/> 40 </TransformGroup> 41 </Image.RenderTransform> 42 </Image> 43 </Canvas> 44 45 <Canvas x:Name="CharaLayer" 46 d:Visibility="Visible" 47 Height="768" Width="1024" 48 Background="Transparent"> 49 <Image x:Name="Char1" 50 Source="char1.png" 51 Height="350" Canvas.Left="9" Canvas.Top="414" HorizontalAlignment="Center" VerticalAlignment="Top"/> 52 <Image x:Name="Char2" 53 Source="char2.png" 54 Height="350" Canvas.Left="767" Canvas.Top="414" HorizontalAlignment="Left" VerticalAlignment="Center"/> 55 </Canvas> 56 </Canvas> 57</Window> 58
ResourceDictionary.xaml
1<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 3 4 <Storyboard x:Key="SampleStoryboard"> 5 <!-- Objectに画像をセット --> 6 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Object" Storyboard.TargetProperty="Source"> 7 <DiscreteObjectKeyFrame KeyTime="0"> 8 <DiscreteObjectKeyFrame.Value> 9 <BitmapImage UriSource="image.png"/> 10 </DiscreteObjectKeyFrame.Value> 11 </DiscreteObjectKeyFrame> 12 </ObjectAnimationUsingKeyFrames> 13 14 <!-- キャラクターフェードイン --> 15 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CharaLayer" Storyboard.TargetProperty="Visibility"> 16 <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 17 </ObjectAnimationUsingKeyFrames> 18 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CharaLayer" 19 Storyboard.TargetProperty="(UIElement.Opacity)"> 20 <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/> 21 <EasingDoubleKeyFrame KeyTime="00:00:01" Value="1"> 22 <EasingDoubleKeyFrame.EasingFunction> 23 <QuarticEase EasingMode="EaseIn"/> 24 </EasingDoubleKeyFrame.EasingFunction> 25 </EasingDoubleKeyFrame> 26 </DoubleAnimationUsingKeyFrames> 27 28 <!-- Objectのアピールイン --> 29 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Object" Storyboard.TargetProperty="(UIElement.Opacity)"> 30 <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/> 31 <EasingDoubleKeyFrame KeyTime="00:00:02" Value="0"/> 32 <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="1"/> 33 </DoubleAnimationUsingKeyFrames> 34 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Object" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 35 <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1.5"/> 36 <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="1"> 37 <EasingDoubleKeyFrame.EasingFunction> 38 <BackEase EasingMode="EaseIn"/> 39 </EasingDoubleKeyFrame.EasingFunction> 40 </EasingDoubleKeyFrame> 41 </DoubleAnimationUsingKeyFrames> 42 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Object" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 43 <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1.5"/> 44 <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="1"> 45 <EasingDoubleKeyFrame.EasingFunction> 46 <BackEase EasingMode="EaseIn"/> 47 </EasingDoubleKeyFrame.EasingFunction> 48 </EasingDoubleKeyFrame> 49 </DoubleAnimationUsingKeyFrames> 50 <!-- 効果音・会話 --> 51 <MediaTimeline Source="se.mp3" Storyboard.TargetName="media" BeginTime="00:00:02"/> 52 </Storyboard> 53 54</ResourceDictionary>
補足情報(FW/ツールのバージョンなど)
Visual Studio 2022
C#10.0
WPF
###追記
コメントで教えていただいた、クローンを使う方法を試しました。
結果、以下の例外が発生しました。
System.InvalidOperationException:
'名前 'Object' を解決するために適用できる名前スコープがありません。'
Storyboard中で、Imageコントロール"Object"を操作しており、Cloneしたインスタンスからコントロールのプロパティを変更しようとするのがまずいのかと考えておりますが、はっきりとはしておらず調査中です。
MainWindow.xaml.cs
1namespace Sample 2{ 3 /// <summary> 4 /// Interaction logic for MainWindow.xaml 5 /// </summary> 6 public partial class MainWindow : Window 7 { 8 public MainWindow() 9 { 10 InitializeComponent(); 11 <!-- 変更箇所 --> 12 var sb = (FindResource("SampleStoryboard") as Storyboard).Clone(); 13 <!-- 変更ここまで --> 14 sb.Completed += SampleSb_Completed; 15 sb.Begin(); 16 } 17 18 private void SampleSb_Completed(object? sender, EventArgs e) 19 { 20 <!-- イベント発生時の処理 --> 21 } 22 } 23}

回答1件
あなたの回答
tips
プレビュー