WPF 按钮点击音效实现
下面我将为您提供一个完整的 WPF 按钮点击音效实现方案,包含多种实现方式和高级功能:
完整实现方案
MainWindow.xaml
<Window x:Class="ButtonClickSound.MainWindow"
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"
xmlns:local="clr-namespace:ButtonClickSound"
mc:Ignorable="d"
Title="按钮点击音效演示"
Height="450"
Width="800"
WindowStartupLocation="CenterScreen"
Background="#FF1E1E1E">
<Window.Resources>
<!-- 音效资源 -->
<MediaPlayer x:Key="ClickSoundPlayer" Source="sounds/click.wav" Volume="0.7"/>
<MediaPlayer x:Key="HoverSoundPlayer" Source="sounds/hover.wav" Volume="0.5"/>
<!-- 按钮样式 -->
<Style x:Key="SoundButtonStyle" TargetType="Button">
<Setter Property="Background" Value="#FF252526"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="#FF3F3F46"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Padding" Value="20,10"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="8">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="{TemplateBinding Padding}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF3A80D2"/>
<Setter Property="BorderBrush" Value="#FF2A70C2"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="#FF2A70C2"/>
<Setter Property="BorderBrush" Value="#FF1A60B2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 标题 -->
<TextBlock Text="按钮点击音效演示"
Grid.Row="0"
FontSize="28"
FontWeight="Bold"
Foreground="White"
HorizontalAlignment="Center"
Margin="0,20"/>
<!-- 按钮区域 -->
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- 简单实现 -->
<StackPanel Grid.Column="0"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="简单实现"
FontSize="20"
Foreground="#AAAAAA"
HorizontalAlignment="Center"
Margin="0,0,0,20"/>
<!-- 直接绑定事件 -->
<Button Content="事件处理器"
Style="{
StaticResource SoundButtonStyle}"
Click="ButtonWithEventHandler_Click"/>
<!-- 使用行为 -->
<Button Content="使用行为"
Style="{
StaticResource SoundButtonStyle}"
local:SoundBehavior.ClickSound="{StaticResource ClickSoundPlayer}"/>
<!-- 使用命令 -->
<Button Content="使用命令"
Style="{
StaticResource SoundButtonStyle}"
Command="{Binding PlaySoundCommand}"/>
</StackPanel>
<!-- 高级实现 -->
<StackPanel Grid.Column="1"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="高级实现"
FontSize="20"
Foreground="#AAAAAA"
HorizontalAlignment="Center"
Margin

最低0.47元/天 解锁文章
1339

被折叠的 条评论
为什么被折叠?



