欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

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

来源: 开发者 投稿于  被查看 25373 次 评论:268

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"
                    Click="PlayVoiceBtn_Click_1">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"></GradientStop>
                        <GradientStop Color="Red" Offset="1"></GradientStop>
                    </LinearGradientBrush>
                </Button.Background>
                音乐</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;
using System.Windows.Media;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Button myButton = new Button();
            myButton.Name="myBtn";
            myButton.Width = 200;
            myButton.Height = 200;
            myButton.Background = new SolidColorBrush(Colors.Red);
            myButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            myButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            myButton.Content = "音乐二";
            CttpnMap.Children.Add(myButton);//将控件加入到grid中
            myButton.Margin = new Thickness(201, 0, 0, 0);
        }
        //单击事件
        private void PlayVoiceBtn_Click_1(object sender, RoutedEventArgs e)
        {
            CuMediaElement.Play();
        }

    }
}

三:注意事项

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

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

四:相关知识点

命名空间和Code-Behind类

    在上文代码中设置了两个XAML命名空间(NameSpace),它们是Visual Studio 2008自动生成的,具体如下所示: 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     设置命名空间的主要目的是帮助XAML解析器了解类所在的位置,其能够减少冲突的可能性。设置命名空间通过xmlns属性来实现,该属性值通常必须是URI(统一资源标志符),例如上面所设置的值。在每个XAML文档中都应该声明类似这样的命名空间。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    是WPF的核心命名空间,其涵盖所有WPF类,包括构建用户界面使用的控件。在本例中,该命名空间没有使用前缀,所以它是整个文档的默认命名空间。换言之,在没有特殊情况下,每个元素都自动归置于该命名空间之下。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    是XAML的命名空间,其包括影响文档解释的各种XAML特性。该命名空间映射为前缀x。这意味着,通过在任何元素名称之前放置这个前缀,都可使用该前缀,例如<x:ElementName>。

    在了解命名空间之后,读者一定还看到在其之上有一行:

<Window x:Class="HelloWpf.Window1"    这行代码表示的并不是命名空间,而是设置所引用的Code-Behind类。HelloWpf.Window1类中包括有关按钮单击的事件处理程序。

    如前文所述,开发人员使用XAML可构建用户界面,同时为了实现业务逻辑还需要撰写程序逻辑代码,例如事件处理程序等。这些程序逻辑代码包含在Code- Behind类文件中。显而易见,必须在XAML与Code-Behind类文件之间建立一定的引用关系,其使用的就是上面那行代码。

    注意:在使用<Window>元素时必须设置x:Class。


自制的wp8应用怎在wp8手机上运行?

请问你写的应用在自己手机上安装测试了吗?怎样才能安装?我也写了个小程序,但也不知道如何安装在自己的手机上测试。
 

有没有没有开发者帐号的,从wp8升级到wp81的?我做到了,而且是入门机htc8s

现在推送的是开发者预览版,去应用商店下载一个开发者预览,注册一下就可以收到推送了。
 

相关文章

    暂无相关文章
相关频道:

用户评论