如何在 XAML 中播放系统声音?
这是一个简单的问题,令我惊讶的是,我找不到答案:如何在 XAML 中播放系统声音?
我有一个附加到按钮的事件触发器。触发器显示一条消息,我希望它播放 Windows 通知声音。我找到了一些关于如何播放声音文件的参考资料,但没有找到关于如何调用系统声音的参考资料。
感谢您的帮助!
Here's a simple question 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SystemSounds
类提供一些系统声音,它们有一个 Play() 方法。要在 XAML 中使用此功能,您要么必须诉诸一些古怪的技巧,实现大量自定义逻辑,要么使用 Blend Interactivity 定义您自己的 TriggerAction,它可以使用 SystemSound 并播放它。交互方法:(
我不知道
SystemSounds.Beep
是否是您正在寻找的方法。)David Veeneman 的注释:
供其他人研究对于此问题,答案中提到的 Blend Interactivity 需要引用 System.Windows.Interactivity.dll,该文件位于
C:\Program Files (x86)\Microsoft SDKs\Expression \Blend\.NETFramework\v4.0\Libraries\
The
SystemSounds
class provides some system sounds, they have aPlay()
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 aSystemSound
and play it.The Interactivity method:
(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\
为了完整起见,下面是我用来实现 HB 问题解决方案的标记。该标记在状态栏中显示一条消息并播放
System.Asterisk
声音。该消息包含在状态栏中名为StatusBarMessagePanel
的StackPanel
中。显示该消息,然后在五秒内消失。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 aStackPanel
namedStatusBarMessagePanel
in the status bar. The message is shown, then faded over a five-second period.在控制面板>下声音>声音您可以找到 SystemSounds 的 .wav 文件的名称。它们位于 Windows\Media 文件夹中。将您的需求文件作为资源添加到您的 WPF 项目中。对于 Asterisk-Sound,例如它是“Windows Background.wav”。那么 xaml 可能如下所示:
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: