Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在XAML中播放系统声音?_Wpf_Xaml - Fatal编程技术网

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

Wpf 如何在XAML中播放系统声音?,wpf,xaml,Wpf,Xaml,让我惊讶的是,这里有一个简单的问题,我找不到答案:如何在XAML中播放系统声音 我有一个事件触发器连接到一个按钮。触发器显示一条消息,我希望它播放Windows通知声音。我找到了一些关于如何播放声音文件的参考资料,但没有找到关于如何调用系统声音的参考资料 谢谢你的帮助 该类提供了一些系统声音,它们有一个Play()方法。要在XAML中使用它,您必须求助于一些古怪的黑客,实现大量自定义逻辑,或者使用混合交互来定义您自己的触发动作,它可以使用SystemSound并播放它 互动方法: 公共类Syst

让我惊讶的是,这里有一个简单的问题,我找不到答案:如何在XAML中播放系统声音

我有一个事件触发器连接到一个按钮。触发器显示一条消息,我希望它播放Windows通知声音。我找到了一些关于如何播放声音文件的参考资料,但没有找到关于如何调用系统声音的参考资料

谢谢你的帮助

该类提供了一些系统声音,它们有一个
Play()
方法。要在XAML中使用它,您必须求助于一些古怪的黑客,实现大量自定义逻辑,或者使用混合交互来定义您自己的触发动作,它可以使用
SystemSound
并播放它

互动方法:

公共类SystemSoundPlayerAction:System.Windows.Interactivity.TriggerAction
{
公共静态只读从属属性SystemSoundProperty=
DependencyProperty.Register(“SystemSound”、typeof(SystemSound)、typeof(SystemSoundPlayerAction)、new UIPropertyMetadata(null));
公共系统声音系统声音
{
获取{return(SystemSound)GetValue(SystemSoundProperty);}
set{SetValue(SystemSoundProperty,value);}
}
受保护的覆盖无效调用(对象参数)
{
if(SystemSound==null)抛出新异常(“未指定系统声音”);
SystemSound.Play();
}
}

...
(我不知道你要找的是不是
SystemSounds.Beep

David Veeneman的注释:


对于研究此问题的其他人,答案中提到的混合交互性需要参考System.Windows.Interactive.dll,完整性请参见
C:\Program Files(x86)\Microsoft SDK\Expression\Blend\.NETFramework\v4.0\Libraries\

,下面是我用来实现HB解决方案的标记。标记在状态栏中显示消息,并播放
System.Asterisk
声音。消息包含在状态栏中名为
StatusBarMessagePanel
StackPanel
中。信息显示,然后在五秒钟内消失

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

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

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


回答得不错!接受和+1.谢谢你;事实上,我在回答这个问题时学到了一些东西:)对于其他研究这个问题的人来说,答案中提到的混合交互性需要参考System.Windows.Interactivity.dll,它位于C:\Program Files(x86)中\Microsoft SDK\Expression\Blend\.NETFramework\v4.0\Libraries\您可以自己在答案中编辑类似的添加内容,因为它是相关的(我添加的),太棒了,我完全忘记了这个问题!
<Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
        <SoundPlayerAction Source="Sounds\WindowsBackground.wav"/>
    </EventTrigger>
</Button.Triggers>