您的位置:知识库 » 手机开发

Windows Phone 7 Tips (2)

作者: Alexis  来源: 博客园  发布时间: 2011-04-19 10:47  阅读: 878 次  推荐: 0   原文链接   [收藏]  

  上一篇Tips:Windows Phone 7 Tips (1), 今天也是分享10个Windows Phone 7 技巧,并提供离线文档下载。
  1. 在Windows Phone 7中最简单的操作IsolatedStorge的代码: SaveNote()将文本框内容保存到Note.txt中,Edit()读取Note.txt到文本框中。

  2. Windows Phone 7中的Orientation。

  分为 Portrait(竖屏)和Landscape(直屏)和PortraitOrLandscape(竖屏或横屏,在切换横竖屏时会自动切换)。并且又分为支持方向和启动方向,支持方向支持Portrait、Landscape和PortraitOrLandscape,启动方向支持Landscape、LandscapeLeft、LandscapeRight、Portrait、PortraitDown和PortraitUp。

  3. Windows Phone 7中常见的UI布局控件比较。

  Canvas:绝对定位控件,速度快,可用于游戏。

  Grid:网格定位控件,比较常用。

  StackPanel:自动定位控件,推荐使用与小区域。

  4. 简单叙述下Windows Phone 7中的生命周期。以下的一张图会对你有所帮助:

  具体的应用程序的事件及什么时候触发如下表: 

Application Event Occurs When Your Actions
Application_Launching The user taps the entry for an application on the installed applications screen, and a new instance of an application is created. Do not read application settings from the isolated storage as that will slow down the loading process; do not attempt to restore transient state. When an application launches, it should always appear as a new instance.
Application_Activated For this event to occur, two conditions must be met: (1) the user navigates away from your application, either by using a launcher or a chooser, or by starting another application and (2) the user then comes back to your application by either completing the launcher or chooser or using the hardware Back button. This event is not raised when an application is first launched. The application should allow the user to continue interaction as if she had never left the application; transient state information should be restored, but the application should not attempt to read the contents of the isolated storage to avoid potential slowdown.
Application_Deactivated The user navigates away from your application either by invoking a launcher or a chooser, or by launching another application. This event is not raised when your application is closing. You should save all transient (i.e., related to the current application session) state into the State dictionary. You should save persistent state to an isolated storage. Applications are given ten seconds to complete this event; after ten seconds, if this event is still not completed, an application will be terminated and not tombstoned.
Application_Closing The user uses the Back key to navigate past the first page of your application.

Save all of the persistent state into the isolated storage. 

  5. 确保你的Windows Phone 7 应用程序的第一个页面在5秒内加载渲染好,在20秒内你的应用程序可用!

  6. Windows Phone 7 中图片为Resource和Content的区别: 生成方式为Resource的图片会被编译到应用程序集(DLL)里,而生成方式为Content的图片则会被部署到XAP包中。

  当你把你的图片copy到项目中,图片的生成动作默认是资源(Resource),但我通常选择Content以获取更快的速度。我们应该如何选择图片的生成方式(Build Action)呢? 每种生成方式都有其用武之地:设置生成方式为Content意味着应用程序可以更快的加载图片资源;将生成方式设为Resource对你重新部署程序非常有用,这对类库项目十分合适的。

  7. Windows Phone 7中的物理回退键:在Tips (1)中曾讲到如何在PhoneApplicationPage_BackKeyPress事件中禁用物理回退键,即e.Cancel = true; 如果我们不编写代码,则当我们点击回退键时,Windows Phone 7会做哪些事情呢?据我所知,有如下三种情况:

  (1). 当你点击回退按钮式,程序必须返回上一个页面。

  (2). 如果你是在程序的第一个页面点击回退按钮,则应用程序必须推出。

  (3). 如果你的应用程序真在现实一个对话框或者Context Menu,点击回退按钮时,Windows Phone 7自动关闭对话框或者 Context Menu。

  8. 在Windows Phone 7 应用程序会有两种不同数据:持久性数据(Persistent Data)和瞬态状态(Transient State)。

  持久性数据:如配置文件(settings)、隔离存储空间(IsolatedStorage)。

  瞬态状态:存在于某个特定的程序回话中,如页面状态,web 请求缓存,瞬态状态以PhoneApplicationService.State 属性存在。

  有效性范围 存储位置 读/写
持久性数据 Phone上的所有应用程序 IsolatedStorage 程序打开/关闭,或者程序运行时
瞬态状态 应用程序回话 PhoneApplicationService的State属性 程序无效/激活时

  9. 我们最好不要在应用程序的Application_Launching和Application_Activated 事件中从隔离存储空间中加载数据,因为这样会使我们的程序启动变慢。

  10. IsolatedStorageSetttings 类提供一个简单序列化数据,我们可以使用现有的序列化方法(如json等)将数据持久到隔离存储空间中。

  CHM文件下载:(41KB)

0
0

手机开发热门文章

    手机开发最新文章

      最新新闻

        热门新闻