天天看點

Windows Phone 7 Tips (4)

1.Windows Phone 7 中常見的使用WebClient代碼段:

    WebClient twitter = new WebClient();

    twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);

    twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + TwitterNameBox.Text));

2. 在Windows Phone 7 程式Deactivated的時候,我們需要處理程式的持久化資料和瞬态資料。

 A。将持久化資料儲存到IsolatedStroage

 B。在App.xaml.cs的Deactivated事件中處理應用程式瞬态資料,将應用程式瞬态資料儲存至PhoneApplicationService.State中

 C。在頁面的OnNavigatedFrom事件中處理頁面瞬态資料,将頁面瞬态資料儲存至PhoneApplicationPage.State中

3. 對于通過WebClient擷取到的XML資料源 ,我們可以使用Linq To XML 友善操作,擷取其屬性以便綁定

 XElement xmlTweets = XElement.Parse(e.Result);

TwitterList.ItemsSource = from tweet in xmlTweets.Descendants("status") select new TwitterItem{message = tweet.Element("text").Value};

4.在使用linq to xml需要注意傳回的xml資料有沒有含命名空間

這個是從tweet search wp7 項目中截取的部分傳回xml資料,注意是傳回xml資料中包含命名空間

Windows Phone 7 Tips (4)
則使用linq to xml 處理資料時也需要加上命名空間,tweet search wp7 項目處理代碼如下
Windows Phone 7 Tips (4)
如果傳回的資料不包含命名空間,如下圖
Windows Phone 7 Tips (4)
5. 在Visual Studio 預設并沒有NavigateToEventHandler的快捷鍵,我們可以自行定義
Windows Phone 7 Tips (4)
選擇菜單欄--> 工具---> 選項,找到 鍵盤節點,在搜尋框中輸入NavigateToEventHandler,會找到EditorContextMenus.XAMLEditor.NavigateToEventHandler,然後按快捷鍵Ctrl+G+T(當然你可以自行定義),點選Assign
Windows Phone 7 Tips (4)
這樣我們就可以使用快捷鍵Ctrl+G+T了,就不需要右擊選擇轉到事件處理,其實我們可以看到快捷鍵已經添加到右擊選項中了
Windows Phone 7 Tips (4)

6. Windows Phone 7 中應用程式設計需要遵循的Three Red Threads: Personal、Relevant、Connected

7. 在Windows Phone7中取得螢幕截圖(轉自http://mxmxm.com)

分享一個取得螢幕截圖的代碼,但是由于程式不能在背景運作,是以隻能通過按鈕或者菜單取得截圖,然後把圖檔儲存在相冊中。

  1. public void CaptureScreen(object sender, EventArgs e)  
  2. {  
  3. WriteableBitmap bmp = new WriteableBitmap(480, 800);  
  4. bmp.Render(App.Current.RootVisual, null);  
  5. bmp.Invalidate();  
  6. MemoryStream stream = new MemoryStream();  
  7. bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 80);  
  8. stream.Seek(0, SeekOrigin.Begin);  
  9. MediaLibrary library = new MediaLibrary();  
  10. string filename = "ScreenShot_" + DateTime.Now.ToString("yyyy-MM-dd_hh:mm:ss");  
  11. library.SavePicture(filename, stream);  
  12. stream.Close();  
  1. VibrateController vib = VibrateController.Default;  
  2. vib.Start(TimeSpan.FromMilliseconds(100));  

靜态資源在第一次編譯後即确定其對象或值,之後不能對其進行修改。動态資源則是在運作時決定,當運作過程中真正需要時,才到資源目标中查找其值。