WPF 按钮点击音效实现

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值