第一个wp7应用程序

现在通过创建一个新的windows phone 7应用程序,来初步了解一下程序中每一个文件的作用。

打开Microsoft Visual Studio 2010,选择“Silverlight for Windows Phone->Windows Phone Application”,并且输入名称为“FirstWP7Program”,这样就创建好了一个wp7应用程序,当然我们这这里不会添加任何代码,主要是了解。解决方案目录如下:

文件Properties\AppMainfest.xml:生成应用程序包所必须的应用程序清单文件。

文件Properties\AssemblyInfo.cs:包含名称和版本的元数据,这些元数据将会被嵌入到生成的程序集中。

文件Properties\WMAppMainfest.xml:一个包含与Windows Phone Silverlight应用程序相关的特定元数据的清单文件,且包含用于Windows Phone的Silverlight所具有的特定功能。

<?xml version="1.0" encoding="utf-8"?>

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.1">
<App xmlns="" ProductID="{f9ae6535-b36c-4f23-8610-14dc43374283}" Title="FirstWP7Program" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="FirstWP7Program author" Description="Sample description" Publisher="FirstWP7Program">
<IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>    <!--说明应用程序图标-->
<Capabilities>
<Capability Name="ID_CAP_GAMERSERVICES"/>
<Capability Name="ID_CAP_IDENTITY_DEVICE"/>
<Capability Name="ID_CAP_IDENTITY_USER"/>
<Capability Name="ID_CAP_LOCATION"/>
<Capability Name="ID_CAP_MEDIALIB"/>
<Capability Name="ID_CAP_MICROPHONE"/>
<Capability Name="ID_CAP_NETWORKING"/>
<Capability Name="ID_CAP_PHONEDIALER"/>
<Capability Name="ID_CAP_PUSH_NOTIFICATION"/>
<Capability Name="ID_CAP_SENSORS"/>
<Capability Name="ID_CAP_WEBBROWSERCOMPONENT"/>
<Capability Name="ID_CAP_ISV_CAMERA"/>
<Capability Name="ID_CAP_CONTACTS"/>
<Capability Name="ID_CAP_APPOINTMENTS"/>
</Capabilities>
<Tasks>
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>    <!--任务name为“_default”,表示启动界面,可以更改启动NavigationPage属性达到调试不同界面的功能-->
</Tasks>
<Tokens>
<PrimaryToken TokenID="FirstWP7ProgramToken" TaskName="_default">
<TemplateType5>
<BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI>    <!--设置背景图片-->
<Count>0</Count>
<Title>FirstWP7Program</Title>    <!--设置标题-->
</TemplateType5>
</PrimaryToken>
</Tokens>
</App>
</Deployment>

 

文件“引用”表示项目集中使用到的一些库文件的列表。

文件App.xaml与App.xaml.cs此处定义程序的入口点,初始化应用程序范围内的相关资源并显示应用程序用户界面。

<Application 
x:Class="FirstWP7Program.App"
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">

<!--应用程序资源--> <!--这里可以定义资源-->
<Application.Resources>
</Application.Resources>

<Application.ApplicationLifetimeObjects>
<!--处理应用程序的生存期事件所需的对象-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated
="Application_Activated" Deactivated="Application_Deactivated"/> <!--在后台代码中可以通过代码操纵者四个事件-->
</Application.ApplicationLifetimeObjects>
</Application>

文件ApplicationIcon.png、Background.png、SplashScreenImage.jpg分别表示为应用程序在手机列表中的图标、在开始页面上的显示的图标、在程序第一次启动时给用户的一个即时的反馈,SplashScreenImage.jpg其实就是当用户点击应用程序到打开应用程序这段时间的一个等待画面,这三幅图片可以更换,更换之后名称需和原来的一样,还有就是这三幅图片的属性“生成操作”必须为“内容(Content)”,否则找不到图片。

文件MainPage.xaml与MainPage.xaml.cs这就是应用程序的主要界面与后台代码文件,当然可以右键进行页面的添加,这和winform程序的从操作是一致的。

<phone:PhoneApplicationPage 
x:Class="FirstWP7Program.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" d:DesignWidth="480" d:DesignHeight="768"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible
="True">

<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent"><!--Grid标签表示网格,这个标签只是负责布局,不会再程序中显示-->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <!--StackPanel标签也是布局的一个标签-->
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/><!--定义应用程序的名称-->
<TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/><!--定义页面的名称-->
</StackPanel>

<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid><!--这里是主要的设计区,如果有必要的话,可以手动删除上面的标题区-->
</Grid>

<!--演示 ApplicationBar 用法的示例代码-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="菜单项 1"/>
<shell:ApplicationBarMenuItem Text="菜单项 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
-->

</phone:PhoneApplicationPage>

在xaml代码的编写中,存在着和html代码一样的嵌套编程,这一点不难理解。上面代码中:

1.<phone:PhoneApplicationPage>标签是Windows Phone应用程序中的顶层标签,它的数据类型为PhoneApplicationFrame,在MainPage.xaml.cs中可以看到页面类的定义“public partial class MainPage : PhoneApplicationPage”,它是继承自PhoneApplicationPage的。

2.标签属性中x:Class属性表示x:Class 可以声明为充当可扩展应用程序标记语言 (XAML) 元素树的根元素并且正在编译的任何元素的属性,也可以声明为已编译应用程序的应用程序定义中的 Application根的属性,语法为:“x:Class="namespace.classname"。

3.好多时候我们会碰到x:Name与Name属性,这两个属性其实是差不多的,区别在类层次不同Name是控件自身提供的,x:Name是FrameworkElement提供的,在一个Control中不能同时使用这两个属性。

4.xaml中命名空间的书写:

在许多可扩展应用程序标记语言 (XAML) 文件的根标记中的命名空间声明内,我们都会发现有两个 XML 命名空间声明。第一个声明将整个 Windows Presentation Foundation (WPF) 命名空间映射为默认命名空间:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"。第二个声明映射单独的可扩展应用程序标记语言 (XAML) 命名空间,通常将其映射为 x: 前缀:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"。

这些声明之间的关系是:XAML 实际上是语言定义,而 WPF 是将 XAML 作为语言使用的一个实现。XAML 语言指定一些为了兼容而假定要实现的语言元素,每个元素都应当能通过针对 XAML 命名空间执行的 XAML 处理器实现进行访问。WPF 实现及其预期的编程模型通常为自己的 API 使用默认的 XML 命名空间,为 XAML 中需要的标记语法使用单独的映射前缀。按照约定,该前缀是 x:,此 x: 约定后面是项目模板、代码示例和此 SDK 中语言功能的文档。XAML 命名空间定义了许多常用功能,这些功能即使对于基本的 WPF 应用程序也是必需的。例如,若要通过分部类将任何代码隐藏加入 XAML 文件,我们必须将该类命名为相关 XAML 文件的根元素中的 x:Class 属性。或者,在 XAML 页中定义的、我们希望作为键控资源访问的任何元素应当对相关元素设置了 x:Key 属性。

在编译完程序之后,在程序根目录下的Bin文件夹之中,我们会找到专用于windows phone 7的xap部署包。

posted on 2012-04-02 16:05  WaitingSky  阅读(740)  评论(0编辑  收藏  举报