如何在XAML中播放系统声音?

问题描述:

下面是一个简单的问题,令我惊讶的是,我找不到答案:如何在XAML中播放系统声音?如何在XAML中播放系统声音?

我有一个附加到按钮的事件触发器。触发器显示一条消息,我希望它播放Windows Notify声音。我发现了几个关于如何播放声音文件的参考文件,但没有提到如何调用系统声音。

感谢您的帮助!

SystemSounds类提供了一些系统声音,他们有一个Play()方法。要在XAML中使用它,你必须使用一些拙劣的黑客,实现大量的自定义逻辑,或者使用Blend Interactivity来定义自己的TriggerAction,它可以使用SystemSound并播放它。

的交互方法:

public class SystemSoundPlayerAction : System.Windows.Interactivity.TriggerAction<Button> 
{ 
    public static readonly DependencyProperty SystemSoundProperty = 
        DependencyProperty.Register("SystemSound", typeof(SystemSound), typeof(SystemSoundPlayerAction), new UIPropertyMetadata(null)); 
    public SystemSound SystemSound 
    { 
     get { return (SystemSound)GetValue(SystemSoundProperty); } 
     set { SetValue(SystemSoundProperty, value); } 
    } 

    protected override void Invoke(object parameter) 
    { 
     if (SystemSound == null) throw new Exception("No system sound was specified"); 
     SystemSound.Play(); 
    } 
} 
<Window 
     xmlns:sysmedia="clr-namespace:System.Media;assembly=System" 
     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"> 
     ... 
     <Button Content="Test2"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <i:EventTrigger.Actions> 
         <local:SystemSoundPlayerAction SystemSound="{x:Static sysmedia:SystemSounds.Beep}"/> 
        </i:EventTrigger.Actions> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 

我不知道SystemSounds.Beep是你正在寻找一个

大卫Veeneman注:

对于其他r esearching这个问题,互动在回答中提到的混合要求System.Windows.Interactivity.dll,这是在C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\

+0

不错的答案!接受和+1。 – 2011-04-15 03:50:07

+2

谢谢;实际上我在回答这个问题时自己学到了一些东西:) – 2011-04-15 03:54:15

+0

对于研究此问题的其他人来说,答案中提到的Blend Interactivity需要参考System.Windows.Interactivity.dll,该文件位于C:\ Program Files(x86) Microsoft SDK \ Expression \ Blend \ .NETFramework \ v4.0 \ Libraries \ – 2011-04-15 10:38:24

为了完整发现了一个参考,这里是我用来实现HB的解决标记问题。标记在状态栏中显示一条消息,并播放声音System.Asterisk。该消息包含在状态栏中名为StatusBarMessagePanelStackPanel中。显示该消息,然后在五秒钟内消失。

<Button ...> 

    <!-- Shows, then fades status bar message. --> 
    <Button.Triggers> 
     <EventTrigger RoutedEvent="Button.Click"> 
      <BeginStoryboard> 
       <Storyboard> 
        <DoubleAnimation From="1.0" To="0.0" Duration="0:0:5"  
           Storyboard.TargetName="StatusBarMessagePanel" 
           Storyboard.TargetProperty="Opacity"/> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </Button.Triggers> 

    <!-- Note that the following markup uses the custom SystemSoundPlayerAction 
    class, which is found in the Utility folder of this project. --> 

    <!-- Plays the System.Asterisk sound --> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Click"> 
      <i:EventTrigger.Actions> 
       <local:SystemSoundPlayerAction SystemSound="{x:Static sysmedia:SystemSounds.Beep}"/> 
      </i:EventTrigger.Actions> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 

</Button>