C# 如何在windows universal app中播放资产中的声音

C# 如何在windows universal app中播放资产中的声音,c#,audio,mediaelement,win-universal-app,C#,Audio,Mediaelement,Win Universal App,我在project.shared中将声音添加到资产中,并在XAML页面中添加了MediaElement 我想在点击图像时播放声音 这是我的代码,但它不起作用 </Grid> <GridView VerticalAlignment="Bottom" Margin="-10,-10,10,10" Height="260" Grid.RowSpan="2"> <StackPanel Orientation="Vertical">

我在project.shared中将声音添加到资产中,并在XAML页面中添加了
MediaElement

我想在点击图像时播放声音

这是我的代码,但它不起作用

</Grid>
    <GridView VerticalAlignment="Bottom" Margin="-10,-10,10,10" Height="260" Grid.RowSpan="2">
        <StackPanel Orientation="Vertical">
            <Image Source="/Assets/Animals/cat.png" Height="200" Width="200" HorizontalAlignment="Center" Tapped="Image_Tapped"/>
            <TextBlock Text="Cat" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30"/>
        </StackPanel>
    </GridView>
    <MediaElement  Name="AudioPlayer" HorizontalAlignment="Left" Height="100" Margin="297,290,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100" Visibility="Collapsed" Volume="1"/>
</Grid> 

首先,您需要定义源文件。您甚至可以通过添加

Source=“mediafile.mp3”

排队

其次,MediaElement的名称是“AudioPlayer”,因此应该从事件处理程序调用

AudioPlayer.Play()


而不是猫。玩()

cat.play()中的
cat
是什么?它不是在XAML中声明的。您还应该处理
MediaElement
的。
private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
    cat.play();
}