一周时间初步学习Windows Phone,假设您已有Silverlight、C#语言等相关基础,在7天中每天最后有一个带源码的案例学习,最后一天是一个综合性的大型案例。

一、 开发环境搭建

相应的开发工具可通过windows phone开发者主页:http://developer.windowsphone.com找到相应的软件,或直接到以下地址下载:

Windows Phone Developer Tools RTW下载地址:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=04704acf-a63a-4f97-952c-8b51b34b00ce&displaylang=en

单击下载链接,vm_web.exe,下载后运行,在线安装,运行(离线安装也可,需下载ISO文件),将自动安装以下相关软件:

The Windows Phone Developer Tools includes the following

1) Visual Studio 2010 Express for Windows Phone

2)Windows Phone Emulator Resources

3)Silverlight 4 Tools For Visual Studio

4)XNA Game Studio 4.0

5)Microsoft Expression Blend for Windows Phone

Windows Phone Developer Tools January 2011 Update:(模拟器等更新)

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=49B9D0C5-6597-4313-912A-F0CCA9C7D277

二、项目模板及文件介绍

Windows Phone包含以下类型的项目:

1) Windows Phone Application:单页面应用程序

2) Windows Phone Databound Application,多页面应用程序

3) Windows Phone Class Library,类库

4) Windows Phone Panorama Applicaion,使用全景控件的应用程序

5)Windows Phone Prvot Application,使用中轴控件应用程序。

以Windows Phone Application项目模板为例,了解一下程序结构,查看解决方案资源管理器,如图1-1。

image

图1-1

包含的主要文件有:

App.xaml:这个文件和ASP.NET中的web.config文件很相似。定义应用程序级的资源、数据和设置等,其中含标记:

<Application.Resources>

………………….

</Application.Resources>

在其代码文件App.xaml.cs中还包含了Application_Launching、Application_Activated、Application_Deactivated、Application_Closing等事件处理。

AppManifest.xml :生成应用程序安装包(XAP)所需的一个简单的应用程序清单文件。

WMAppManifest.xml :包含了应用程序特定数据的元数据文件,包括标题,图标位置,功能等等

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

Background.png :当程序被显示到开始界面中就会显示这个图像。MainPage.xaml :程序首页面。

SplashScreenImage.jpg :应用程序载入时显示的图片。

ApplicationIcon.png:程序特定的图标,用户会在他们的程序中将看到此图标。

三、一些学习资源
MSDN手机开发:
http://msdn.microsoft.com/zh-cn/ff380145
http://msdn.microsoft.com/zh-cn/windowsphone/default.aspx?ocid=ban-f-cn-wp7--MSDN
51CTO Windows Phone专题:http://mobile.51cto.com/mobile-219051.htm
台湾MSDN:http://msdn.microsoft.com/zh-tw/windowsphone/ff955788
Silverlight for WP7视频:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/Series/WP.aspx
WP7书籍:
http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=Phone+7

银光wp7:ghttp://silverlightchina.net/html/wp7/

<>

案例1-1:完成一个经典的“Hello”应用程序

完成一个经典的“Hello”应用程序 ,创建一个针对Windows Phone的Silverlight的应用程序,学习如何使用相应开发工具来创建和设计Windows phone应用程序,并在Windows Phone Emulator上去部署。

(1)启动Visual Studio 2010,在项目对话框中,选择模板“Silverlight for Windows Phone”,创建“Windows Phone Application”

(2)在资源管理器中,可看到解决方案MyHello中项目,及文件结构。如图1-2。

image

图1-2

MainPage.xaml为程序的起始页面,默认界面xaml代码如下:

<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 x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!--ContentPanel - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>

</Grid>

页面分成了二个部分,Grid容器中含有StackPanel、Grid控件,其中StackPanel中为程序标题信息。自动生成两个TextBlock,一个显示”MY APPLICATION”,这是应用程序的名称;另一个显示page name“,这是当前页的标题。可修改这些显示,使之反映自己的实际程序,先将当前页的标题名称”page name”改为”Hello”,暂时不作任何改动。

(3)选择模拟器输出方式:Windows Phone 7 Emulator

(4)按”F6”编译程序,将创建模拟器,并在模拟器窗口中显示程序的启动页面,应该是没有任何错误。

(5)按”F5”运行程序,将创建模拟器显示应用程序,第一次启动,时间要长些。效果如图1-3:

image

图1-3

(6)按”SHIFT+F5”结束运行,模拟器最好不要关闭,那么部署新应用程序来进行测试时,模拟器会更快地响应。

(7)设计用户界面,打开工具箱直接拖动控件即可。添加一个textblock、一个textbox、 及一个button控件,xaml标记如下,已直接添加注解:

<Grid x:Name="LayoutRoot" Background="Transparent" ShowGridLines="True">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto" />//增加一行

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<!-- 第一行为标题 -->

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="Hello" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!—第二行,定义为两列,放置两个控件,第1列宽度设置为”*”,尽可能伸展宽度,第2列宽度设置为”Auto”根据控件的宽度而调整 -->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<Grid.ColumnDefinitions >

<ColumnDefinition Width="*" />

<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>

<TextBox Grid.Column="0" Name="textBox1" Text="" Margin="20" ontSize="{StaticResource PhoneFontSizeExtraLarge}" />// FontSize属性值读取资源

<Button Content="点击" HorizontalAlignment="Right" Grid.Column="1" Name="button1" Margin="20" Padding="8" Click="button1_Click" />

</Grid>

<!—增加第三行,显示用户输入, HorizontalAlignment为伸展Stretch -->

<Grid Grid.Row="2" Name="grid1" VerticalAlignment="Top" >

<TextBlock Name="textBlock1" Text="" Style="{StaticResource PhoneTextExtraLargeStyle}" HorizontalAlignment="Stretch" Foreground="Red" TextWrapping="NoWrap" TextAlignment="Center" FontWeight="Bold" />

</Grid>

</Grid>

</Grid>

(8)双击命令按钮,创建事件处理函数,在代码隐藏文件中对应的单击事件处理程序中插入代码,如下所示:

private void button1_Click(object sender, RoutedEventArgs e)

{

textBlock1.Text = textBox1.Text;

textBox1.Text = String.Empty;

}

(9)按F5键运行程序,在文本框中输入后,单击 “单击我”按钮,将会在第二个TextBlock显示输入的内容。显示效果如图1-4:

image

图1-4

(10)如需调试应用程序,切换到Visual Studio,将光标定位到下面这一行:

textBlock1.Text = textBox1.Text;

按F9或单击该行左边,插入断点,会看到页边显示一个红点。

(11)F5运行后,Visual Studio会停止于断点行,这时可查看相应的变量值等,可进一步调试程序。

7天学习主要参考资料如下,并特此鸣谢!

1. WP7TrainingKitOffline

2. 林永坚.Silverlight for Windows Phone 开发系列

3. 张崟.Windows Phone 7 开发 31 日谈

4. 306ROOM(网名).一起学windows phone7