天天看點

Windows Phone 7 Tips (2)

上一篇Tips:Windows Phone 7 Tips (1), 今天也是分享10個Windows Phone 7 技巧,并提供離線文檔下載下傳

1. 在Windows Phone 7中最簡單的操作IsolatedStorge的代碼: SaveNote()将文本框内容儲存到Note.txt中,Edit()讀取Note.txt到文本框中

Windows Phone 7 Tips (2)

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中的生命周期。以下的一張圖會對你有所幫助

Windows Phone 7 Tips (2)
具體的應用程式的事件及什麼時候觸發如下表: 

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等)将資料持久到隔離存儲空間中