2011-11-11 53 views
1

所以我試圖創建飛行控制(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?

+0

文檔說操作是BeginStoryboard對象的集合,所以也許PlaySoundAction只是不符合? http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.actions(v=VS.95).aspx –

回答

5

也許它會幫助您知道您正在嘗試在代碼中使用System.Windows.EventTrigger而不是System.Windows.Interactivity.EventTrigger。當我明確指定了 - 我得到它的工作:

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,它需要有一個補刷集。不確定您的自定義控件的功能。

這是我的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> 
+0

嗯,一旦有機會,我必須檢查一下。 – ShelbyZ

+0

這使我可以確實添加EventTrigger,但讓它觸發似乎是另一個問題。 – ShelbyZ

+0

我把我的解決方案在這裏: http://dl.dropbox.com/u/1192076/PlaySoundActionTest.zip 也添加XAML到我的答案。 –

相關問題