Adding sound to buttons with a ResourceDictionary
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.
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.
Code: Select all
<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>
Code: Select all
<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.
-
-
sfernandez
Site Admin
- Posts: 2727
- Joined:
Re: Adding sound to buttons with a ResourceDictionary
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:
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:
Code: Select all
<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>
Code: Select all
<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>
Re: Adding sound to buttons with a ResourceDictionary
Hi,
thanks it worked.
In my example one EventTrigger was missing the b: prefix, which I edited in. With that it works fine :D
thanks it worked.
In my example one EventTrigger was missing the b: prefix, which I edited in. With that it works fine :D
-
-
sfernandez
Site Admin
- Posts: 2727
- Joined:
Re: Adding sound to buttons with a ResourceDictionary
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: Ahrefs [Bot] and 0 guests