2011-04-15 7 views
6

Aquí hay una pregunta simple que para mi sorpresa, no puedo encontrar la respuesta a: ¿Cómo reproduzco un sistema de sonido en XAML?¿Cómo reproduzco un sonido de sistema en XAML?

Tengo un activador de evento asociado a un botón. El disparador muestra un mensaje y quiero que reproduzca el sonido de notificación de Windows. He encontrado varias referencias sobre cómo reproducir archivos de sonido , pero nada sobre cómo invocar un sistema de sonido.

Gracias por su ayuda!

Respuesta

8

La clase SystemSounds proporciona algunos sonidos del sistema, tienen un método Play(). Para usar esto en XAML tendrías que recurrir a algunos hacks extraños, implementar mucha lógica personalizada o usar Blend Interactivity para definir tu propia TriggerAction que puede usar un SystemSound y reproducirlo. Método

La interactividad:

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> 

(. no sé si SystemSounds.Beep es el que usted está buscando)

Nota de David Veeneman:

Para otros r esearching esta cuestión, la mezcla interactividad se hace referencia en la respuesta requiere una referencia a System.Windows.Interactivity.dll, que se encuentra en C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\

+0

¡Respuesta agradable! Aceptado y +1. –

+2

Gracias; de hecho, aprendí algo al responder esto :) –

+0

Para otros que investigan este problema, la interactividad de mezcla a la que se hace referencia en la respuesta requiere una referencia a System.Windows.Interactivity.dll, que se encuentra en C: \ Archivos de programa (x86) \ Microsoft SDKs \ Expression \ Blend \ .NETFramework \ v4.0 \ Libraries \ –

1

Para completar, aquí es el margen de beneficio que solía poner en práctica la solución de HB a la problema. El marcado muestra un mensaje en la barra de estado y reproduce el sonido System.Asterisk. El mensaje está contenido en un StackPanel llamado StatusBarMessagePanel en la barra de estado. El mensaje se muestra, luego se desvaneció durante un período de cinco segundos.

<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> 
Cuestiones relacionadas