简体   繁体   English

在整个应用程序中按下按钮时播放声音

[英]Play a sound when button pressed application wide

I have a WPF application that should be used on a touch screen. 我有一个WPF应用程序,应该在触摸屏上使用。 I have been asked to play a sound when a button is pressed on the screen, for the whole application. 在整个应用程序中,我被要求在屏幕上按下按钮时播放声音。 My idea was to create a style to do this, include the resource dictionnary in my app.It works quite well. 我的想法是创建一种样式来做到这一点,在我的应用程序中包含资源字典,效果很好。 However, this style is in a separate DLL and I would like my main application to change the path of the sound file (BoutonClickSound Url below). 但是,此样式位于单独的DLL中,我希望主应用程序更改声音文件的路径(下面的BoutonClickSound Url)。 But I could not find a way to do it. 但是我找不到办法。 I have tried several ugly things like : 我已经尝试过一些丑陋的事情,例如:

    System.Windows.Application.Current.Resources.MergedDictionaries[0]["BoutonClickSound"] = new Uri(m_MainVM.ButClickSoundPath);
System.Windows.Application.Current.Resources.MergedDictionaries[0].MergedDictionaries[0]["BoutonClickSound"] = new Uri(m_MainVM.ButClickSoundPath);

But none of them seems to work... 但是它们似乎都不起作用...

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                xmlns:sys="clr-namespace:System;assembly=System">

<sys:Uri x:Key="BoutonClickSound">c:\buttonclick.wav</sys:Uri>

<Style TargetType="{x:Type FrameworkElement}"  x:Key="SoundButton">
    <Style.Triggers>
        <EventTrigger RoutedEvent="PreviewMouseDown">
            <SoundPlayerAction x:Name="SoundPlayer" Source="{DynamicResource BoutonClickSound}" />
        </EventTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type primitives:Selector}"  x:Key="SoundSelectionChange">
    <Style.Triggers>
        <EventTrigger RoutedEvent="SelectionChanged">
            <SoundPlayerAction x:Name="SoundPlayer" Source="{DynamicResource BoutonClickSound}" />
        </EventTrigger>
    </Style.Triggers>
</Style>

Any idea how to do this ? 任何想法如何做到这一点?

Thank you. 谢谢。

Just declare a local resource with the same key. 只需声明具有相同密钥的本地资源即可。

If you want it globally in your application, you can add it to a MergedDictionary after you include the MergedDictionary from your library. 如果要在应用程序中全局使用它,则可以在将库中的MergedDictionary包括在内之后将其添加到MergedDictionary中。

Add another entry like this: 添加另一个像这样的条目:

<sys:Uri x:Key="BoutonClickSound">c:\differentfile.wav</sys:Uri>

And it should cause your style to use that file instead. 并且它应该使您的样式改用该文件。

Edit: 编辑:

To set the resource in the code-behind, first you must add it to the local resource dictionary: 要在后面的代码中设置资源,首先必须将其添加到本地资源字典中:

Resources.Add("BoutonClickSound", new Uri("your uri here"));

After you add it, if you want to later change it, you can't re-add it, you have to modify it: 添加后,如果要稍后对其进行更改,则无法重新添加,必须对其进行修改:

Resources["BoutonClickSound"] = new Uri("your uri here");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM