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

发布于 2024-11-01 02:31:43 字数 162 浏览 2 评论 0原文

这是一个简单的问题,令我惊讶的是,我找不到答案:如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-11-08 02:31:43

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 是否是您正在寻找的方法。

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 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\

沫离伤花 2024-11-08 02:31:43

为了完整起见,下面是我用来实现 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>

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 StatusBarMessagePanelin 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>
檐上三寸雪 2024-11-08 02:31:43

在控制面板>下声音>声音您可以找到 SystemSounds 的 .wav 文件的名称。它们位于 Windows\Media 文件夹中。将您的需求文件作为资源添加到您的 WPF 项目中。对于 Asterisk-Sound,例如它是“Windows Background.wav”。那么 xaml 可能如下所示:

<Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
        <SoundPlayerAction Source="Sounds\WindowsBackground.wav"/>
    </EventTrigger>
</Button.Triggers>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文