首页 > 代码库 > WP8开发--------------我的第一个wp8小程序

WP8开发--------------我的第一个wp8小程序

一:截图,功能介绍:点击音乐红色按钮,播放铃声

二:代码

XAML代码
<phone:PhoneApplicationPage    x:Class="PhoneApp1.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    FontFamily="{StaticResource PhoneFontFamilyNormal}"    FontSize="{StaticResource PhoneFontSizeNormal}"    Foreground="{StaticResource PhoneForegroundBrush}"    SupportedOrientations="Portrait" Orientation="Portrait"    shell:SystemTray.IsVisible="True">    <!--LayoutRoot is the root grid where all page content is placed-->    <Grid x:Name="LayoutRoot" Background="Transparent">        <Grid.RowDefinitions>            <RowDefinition Height="Auto"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <!--TitlePanel contains the name of the application and page title-->        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">            <TextBlock Text="红马車" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>            <TextBlock Text="hongmaju" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>        </StackPanel>        <!--ContentPanel - place additional content here-->        <Grid x:Name="CttpnMap" Grid.Row="1" Margin="12,0,12,0">            <Button Name="PlayVoiceBtn"                    Height="200"                    Width="150"                     HorizontalAlignment="Left"                    VerticalAlignment="Center"                     Margin="0,-400,0,0"                    Background="Red"                    Click="PlayVoiceBtn_Click_1">                音乐</Button>            <MediaElement x:Name="CuMediaElement"                          Source="/Assets/Voices/Alarm01.wav"                          Volume="1"                          AutoPlay="False"                ></MediaElement>        </Grid>    </Grid></phone:PhoneApplicationPage>
后台代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Navigation;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;using PhoneApp1.Resources;namespace PhoneApp1{    public partial class MainPage : PhoneApplicationPage    {        // Constructor        public MainPage()        {            InitializeComponent();            // Sample code to localize the ApplicationBar            //BuildLocalizedApplicationBar();        }        //单击事件        private void PlayVoiceBtn_Click_1(object sender, RoutedEventArgs e)        {            CuMediaElement.Play();        }        // Sample code for building a localized ApplicationBar        //private void BuildLocalizedApplicationBar()        //{        //    // Set the page‘s ApplicationBar to a new instance of ApplicationBar.        //    ApplicationBar = new ApplicationBar();        //    // Create a new button and set the text value to the localized string from AppResources.        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));        //    appBarButton.Text = AppResources.AppBarButtonText;        //    ApplicationBar.Buttons.Add(appBarButton);        //    // Create a new menu item with the localized string from AppResources.        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);        //    ApplicationBar.MenuItems.Add(appBarMenuItem);        //}    }}

三:注意事项

如果不能实现单机播放铃声,可以查看铃声的路径是否正确,路径不使用反斜杠“\”

XAML代码,btn的代码和media元素代码必须放在同一级的同一个grid中,不能将一个和另一个所在的grid并列放到同一级的grid中

WP8开发--------------我的第一个wp8小程序