DavidG
Topic Author
Posts: 11
Joined: 06 Dec 2022, 12:14

Adding sound to buttons with a ResourceDictionary

18 Jan 2023, 11:23

Hi,

so I've recently learned about ResourceDictionary's. I've taken a button from a Noesis Example which plays a sound if you hover over them.
I want all my buttons to make this sound on hover, so I want to use a ResourceDictionary to do it.
I've tried multiple things, but I'm still getting error messages. My most recent attempt can be seen below.
Is there a way to achieve this?
Thanks in advance.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions" xmlns:b1="clr-namespace:Noesis;assembly=Noesis.GUI">

    <ControlTemplate TargetType="Button" x:Key="InteractiveButton">

        <ControlTemplate.Triggers>
            <b:EventTrigger EventName="MouseEnter">
                <noesis:SetFocusAction/>
            </b:EventTrigger>
            <b:EventTrigger EventName="GotFocus">
                <b:PlaySoundAction Source="../../Art/Audio/Sounds/ButtonHover.mp3" Volume="1.0"/>
            </b:EventTrigger>
            <b:EventTrigger EventName="Click">
                <b:PlaySoundAction Source="../../Art/Audio/Sounds/ButtonClick.mp3" Volume="1.0"/>
            </b:EventTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>
<UserControl x:Class="MyEngineBuilder.MainMenuView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
    d:DesignWidth="1280" d:DesignHeight="720"
    Focusable="True">
    <UserControl.Resources>
        <ResourceDictionary Source="../ButtonDictionary.xaml"/>
    </UserControl.Resources>
    <Button>I want to make a sound!<Button/>
<UserControl/>
    
    
Last edited by DavidG on 19 Jan 2023, 07:51, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Adding sound to buttons with a ResourceDictionary

18 Jan 2023, 20:13

If you want to reuse interactivity triggers in several instances of a control you can define them in the Style by using the StyleInteraction attached property.
For example, in our QuestLog sample we are doing that: https://github.com/Noesis/Tutorials/blo ... #L317-L326

We will update our Buttons sample because it makes more sense to define it this way:
<Style TargetType="{x:Type Button}">
    ...
    <Setter Property="noesis:StyleInteraction.Triggers">
        <Setter.Value>
          <noesis:StyleTriggerCollection>
            <b:EventTrigger EventName="MouseEnter">
                <noesis:SetFocusAction/>
            </b:EventTrigger>
            <b:EventTrigger EventName="GotFocus">
                <b:PlaySoundAction Source="AudioSlide.mp3" Volume="0.2"/>
            </b:EventTrigger>
            <b:EventTrigger EventName="Click">
                <b:PlaySoundAction Source="AudioClick.mp3" Volume="0.3"/>
            </b:EventTrigger>
          </noesis:StyleTriggerCollection>
        </Setter.Value>
    </Setter>
</Style>
<Grid>
    ...
    <Button x:Name="StartButton" Grid.Row="1" Content="START" Margin="60,0,0,0" Command="{Binding StartCommand}"/>
    <Button x:Name="SettingsButton" Grid.Row="3" Content="SETTINGS" Margin="60,0,0,0" Command="{Binding SettingsCommand}"/>
    <Button x:Name="ExitButton" Grid.Row="5" Content="EXIT" Margin="60,0,0,0" Command="{Binding ExitCommand}"/>
</Grid>
 
DavidG
Topic Author
Posts: 11
Joined: 06 Dec 2022, 12:14

Re: Adding sound to buttons with a ResourceDictionary

19 Jan 2023, 07:53

Hi,
thanks it worked.
In my example one EventTrigger was missing the b: prefix, which I edited in. With that it works fine :D
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Adding sound to buttons with a ResourceDictionary

19 Jan 2023, 14:15

You're right, I edited my xaml to fix the missing b: prefix. Marking this as solved then.

Who is online

Users browsing this forum: No registered users and 54 guests