如何在后面的代码中添加带有 SoundActions 的 EventTrigger? [英] How to Add EventTrigger with SoundActions in code behind?
问题描述
因此,我正在尝试动态创建控件(准确地说是 RegularPolygon),并且我想将 2 个 PlaySoundAction 作为基于 Tap 事件的 EventTrigger 添加到控件中.目前我有以下代码:
So I'm trying to create controls on the fly (RegularPolygon to be exact) and I want to add 2 PlaySoundActions to the controls as an EventTrigger based off of the Tap Event. Currently I have the following code:
EventTrigger trigger = new EventTrigger();
PlaySoundAction correct = new PlaySoundAction();
PlaySoundAction incorrect = new PlaySoundAction();
correct.Source = new Uri("/Sounds/Correct.mp3");
correct.Volume = 0.5;
incorrect.Source = new Uri("/Sounds/Incorrect.mp3");
incorrect.Volume = 0.5;
trigger.Actions.Add(correct); // this line doesn't work
trigger.Actions.Add(incorrect); // this also doesn't work
shape.Triggers.Add(trigger);
每一行都有一个类似的错误
Each line has an error like
错误 2 参数 1:无法从'Microsoft.Expression.Interactivity.Media.PlaySoundAction' 到'System.Windows.TriggerAction'
Error 2 Argument 1: cannot convert from 'Microsoft.Expression.Interactivity.Media.PlaySoundAction' to 'System.Windows.TriggerAction'
我不完全确定将 PlaySoundAction 对象转换为什么.我不想在 XAML 中执行此操作,因为我正在即时创建这些控件.
I'm not entirely sure what to cast the PlaySoundAction object as. I don't want to do this in XAML because I'm creating these controls on the fly.
我还尝试为 RegularPolygon 创建一个样式,让 EventTrigger 和 PlaySoundAction(s),但以编程方式设置控件的样式不会将此逻辑添加到控件中.
I also tried creating a Style for RegularPolygon to have the EventTrigger with the PlaySoundAction(s), but setting the style of the control programmatically does not add this logic to the control.
<Application.Resources>
<ResourceDictionary>
<Style TargetType="es:RegularPolygon" x:Key="Default">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<eim:PlaySoundAction Source="/Sounds/Incorrect.mp3" Volume="0.5" />
<eim:PlaySoundAction Source="/Sounds/Correct.mp3" Volume="0.5" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Style>
</ResourceDictionary>
</Application.Resources>
有没有办法在后面的代码中添加 EventTrigger/PlaySoundAction 或创建一个控件可以继承的具有 EventTrigger/PlaySoundAction 的样式?
推荐答案
了解您正在尝试在代码中使用 System.Windows.EventTrigger 而不是 System.Windows.Interactivity.EventTrigger 可能会有所帮助.当我明确指定时 - 我让它工作了:
Perhaps it would help to know that you are trying to use System.Windows.EventTrigger instead of System.Windows.Interactivity.EventTrigger in your code. When I explicitly specified that - I got it to work:
System.Windows.Interactivity.EventTrigger trigger =
new System.Windows.Interactivity.EventTrigger();
trigger.EventName = "MouseLeftButtonDown";
PlaySoundAction correct = new PlaySoundAction();
correct.Source = new Uri("/Sample.wma", UriKind.Relative);
correct.Volume = 1.0;
trigger.Actions.Add(correct);
trigger.Attach(myTextBlock);
您需要确保您的控件也可以被点击 - IsHitTestVisible 不能设置为 false,并且需要设置填充画笔.不确定您的自定义控件的作用.
You need to make sure that your control can be tapped too - IsHitTestVisible can't be set to false and it needs to have a fill brush set. Not sure what your custom control does.
这是我的 XAML:
<Grid
x:Name="ContentPanel"
Background="LightCoral"
Tap="ContentPanel_Tap"
Grid.Row="1"
Margin="12,0,12,0" >
<StackPanel>
<TextBlock
Text="XAML Test">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="MouseLeftButtonDown">
<eim:PlaySoundAction
Source="/Balloon.wav"
Volume="1" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
<TextBlock
Margin="0,100,0,0"
x:Name="myTextBlock"
Text="Coded Test" />
</StackPanel>
</Grid>
这篇关于如何在后面的代码中添加带有 SoundActions 的 EventTrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!