Win 8.1 RT:我想为按钮点击事件播放点击声音

Win 8.1 RT: I want to play a click sound for a button click event

在我正在使用的 Win RT 应用程序中,我有一个数字键盘用户控件。 用户要求每次按下数字时,他都希望听到咔嗒声。我是 Win RT 的新手,不知道该怎么做。

我的用户控件如下;

<UserControl.Resources>

    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="NumberItem" TargetType="StackPanel">
            <Setter Property="Background" Value="Gray" />
            <Setter Property="Height" Value="50" />
            <Setter Property="Width" Value="50" />
        </Style>

        <Style x:Key="NumberItemText" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="Margin" Value="0, 8, 0, 0" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="26" />
        </Style>

        <x:Double x:Key="WideBtnWidth">110</x:Double>
        <x:Double x:Key="FullBtnWidth">170</x:Double>

        <Style x:Key="ResultItem" TargetType="StackPanel">
            <Setter Property="Background" Value="White" />
            <Setter Property="Height" Value="35" />
            <Setter Property="Width" Value="170" />
            <Setter Property="Margin" Value="0, 0, 0, 8" />
        </Style>

        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />

    </ResourceDictionary>

</UserControl.Resources>

<StackPanel>
    <Button x:Name="btn">
        <Button.Template>
            <ControlTemplate>
                <StackPanel Background="Transparent" Width="100">
                    <TextBlock HorizontalAlignment="Right" Text="&#xE138;" Foreground="{StaticResource ResourceKey=MulalleyBlueBrush}" FontSize="28" FontFamily="Segoe UI Symbol" />
                </StackPanel>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Popup x:Name="ParentedPopup" IsLightDismissEnabled="True" HorizontalOffset="0" VerticalOffset="0" Opened="ParentedPopup_Opened">
        <StackPanel Height="350" Width="190" x:Name="PopupContent">
            <StackPanel.Background>
                <SolidColorBrush Color="Black" />
            </StackPanel.Background>

            <Grid Margin="12" Background="Transparent">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Row="0" Grid.ColumnSpan="3" Grid.Column="0" Style="{StaticResource ResourceKey=ResultItem}">
                    <TextBlock Text="{Binding Path=InternalResult, ElementName=Ctl, Mode=TwoWay}" HorizontalAlignment="Right" Margin="0, 4, 10, 0" FontSize="20" />
                </StackPanel>

                <Button Grid.Row="1" Grid.Column="0" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="1">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="1" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="1" Grid.Column="1" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="2">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="2" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="1" Grid.Column="2" Margin="0, 0, 0, 8" Click="Button_Click" CommandParameter="3">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="3" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="2" Grid.Column="0" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="4">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="4" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="2" Grid.Column="1" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="5">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="5" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="2" Grid.Column="2" Margin="0, 0, 0, 8" Click="Button_Click" CommandParameter="6">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="6" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="3" Grid.Column="0" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="7">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="7" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="3" Grid.Column="1" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="8">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="8" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="3" Grid.Column="2" Margin="0, 0, 0, 8" Click="Button_Click" CommandParameter="9">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="9" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="0, 0, 8, 8" Click="Button_Click" CommandParameter="0" Visibility="{Binding Path=IsInt, ElementName=Ctl, Converter={StaticResource ResourceKey=InverseBooleanToVisibilityConverter}}">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}" Width="{StaticResource ResourceKey=WideBtnWidth}">
                                <TextBlock Text="0" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="4" Grid.Column="2" Margin="0, 0, 0, 8" Click="Button_Click" CommandParameter="." Visibility="{Binding Path=IsInt, ElementName=Ctl, Converter={StaticResource ResourceKey=InverseBooleanToVisibilityConverter}}">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="." Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" Margin="0, 0, 0, 8" Click="Button_Click" CommandParameter="0" Visibility="{Binding Path=IsInt, ElementName=Ctl, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}" Width="{StaticResource ResourceKey=FullBtnWidth}">
                                <TextBlock Text="0" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>


                <Button Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Margin="0, 0, 8, 0" x:Name="Enter" Click="Enter_Click">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}" Width="{StaticResource ResourceKey=WideBtnWidth}">
                                <TextBlock Text="Enter" Margin="0, 10, 0, 0" FontSize="20" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Grid.Row="5" Grid.Column="2" Margin="0, 0, 0, 0" x:Name="Back" Click="Back_Click">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel Style="{StaticResource ResourceKey=NumberItem}">
                                <TextBlock Text="&#xE112;" Margin="0, 12, 0, 0" FontSize="20" FontFamily="Segoe UI Symbol" Style="{StaticResource ResourceKey=NumberItemText}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

            </Grid>

        </StackPanel>
    </Popup>
</StackPanel>

在您的 xaml 代码中添加媒体元素

 <MediaElement x:Name="bleep" Source="Assets/bleep.wav" AutoPlay="False" Visibility="Collapsed"/>

使用您的点击声音和按钮处理程序中的添加更改来源

bleep.Play()