Итак, я пытаюсь создать элементы управления на лету (точнее, RegularPolygon) и хочу добавить 2 элемента PlaySoundActions к элементам управления в качестве EventTrigger на основе события Tap. В настоящее время у меня есть следующий код:
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);
В каждой строке есть ошибка типа
Ошибка 2 Аргумент 1: невозможно преобразовать из
«Microsoft.Expression.Interactivity.Media.PlaySoundAction» для
'System.Windows.TriggerAction'
Я не совсем уверен, что именно для приведения объекта PlaySoundAction. Я не хочу делать это в XAML, потому что я создаю эти элементы управления на лету.
Я также пытался создать стиль для RegularPolygon, чтобы EventTrigger имел PlaySoundAction (s), но программная установка стиля элемента управления не добавляет эту логику в элемент управления.
<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?