How do I play a System Sound in XAML?
Here's a simple ques开发者_如何学Pythontion that to my surprise, I can't find the answer to: How do I play a system sound in XAML?
I have an event trigger attached to a button. The trigger displays a message, and I want it to play the Windows Notify sound. I have found several references on how to play sound files, but nothing on how to invoke a system sound.
Thanks for your help!
The SystemSounds
class provides some system sounds, they have a Play()
method. To use this in XAML you would either have to resort to some whacky hacks, implement lots of custom logic or use Blend Interactivity to define your own TriggerAction which can use a SystemSound
and play it.
The Interactivity method:
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>
(I do not know if SystemSounds.Beep
is the one you are looking for.)
David Veeneman's Note:
For others researching this issue, the Blend Interactivity referred to in the answer requires a reference to System.Windows.Interactivity.dll, which is found in C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\
For completeness, here is the markup I used to implement HB's solution to the problem. The markup shows a message in the status bar and plays the System.Asterisk
sound. The message is contained in a StackPanel
named StatusBarMessagePanel
in the status bar. The message is shown, then faded over a five-second period.
<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>
Under ControlPanel > Sound > Sounds you find the names of the .wav files of the SystemSounds. They are located in the Windows\Media folder. Add the file of your demand as Resource to your WPF-Project. For the Asterisk-Sound e.g. it is 'Windows Background.wav'. Then xaml could look like this:
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<SoundPlayerAction Source="Sounds\WindowsBackground.wav"/>
</EventTrigger>
</Button.Triggers>
精彩评论