所以我試圖創建飛行控制(RegularPolygon是準確的),我想添加2個PlaySoundActions作爲基於Tap事件的EventTrigger作爲控件。目前,我有以下代碼:如何在後面的代碼中添加帶有SoundAction的EventTrigger?
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」轉換爲 「系統.Windows.TriggerAction'
我不完全確定要將PlaySoundAction對象轉換爲什麼。我不想在XAML中這樣做,因爲我正在創建這些控件。
我也嘗試創建一個RegularPolygon風格與PlaySoundAction(s)EventTrigger,但以編程方式設置控件的樣式不會將此邏輯添加到控件。
<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?
文檔說操作是BeginStoryboard對象的集合,所以也許PlaySoundAction只是不符合? http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.actions(v=VS.95).aspx –