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)_第1张图片   则使用linq to xml 处理数据时也需要加上命名空间,tweet search wp7 项目处理代码如:

Windows Phone 7 Tips (4)_第2张图片

  如果返回的数据不包含命名空间,如下:

Windows Phone 7 Tips (4)_第3张图片

 

  则处理代码如下: Windows Phone 7 Tips (4)_第4张图片

  5、在Visual Studio 默认并没有NavigateToEventHandler的快捷键,我们可以自行定义:

Windows Phone 7 Tips (4)_第5张图片   选择菜单栏--> 工具---> 选项,找到 键盘节点,在搜索框中输入NavigateToEventHandler,会找到EditorContextMenus.XAMLEditor.NavigateToEventHandler,然后按快捷键Ctrl+G+T(当然你可以自行定义),点击Assign:

Windows Phone 7 Tips (4)_第6张图片   这样我们就可以使用快捷键Ctrl+G+T了,就不需要右击选择转到事件处理,其实我们可以看到快捷键已经添加到右击选项中了:

Windows Phone 7 Tips (4)_第7张图片

  6、Windows Phone 7 中应用程序设计需要遵循的Three Red Threads: Personal、Relevant、Connected。

  7、在Windows Phone7中取得屏幕截图(转自http://mxmxm.com)。

  分享一个取得屏幕截图的代码,但是由于程序不能在后台运行,所以只能通过按钮或者菜单取得截图,然后把图片保存在相册中。

   
   
   
   
public void CaptureScreen( object sender, EventArgs e)
{
  WriteableBitmap bmp
= new WriteableBitmap( 480 , 800 );
  bmp.Render(App.Current.RootVisual,
null );
  bmp.Invalidate();

  MemoryStream stream
= new MemoryStream();
  bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight,
0 , 80 );
  stream.Seek(
0 , SeekOrigin.Begin);

  MediaLibrary library
= new MediaLibrary();
  string filename = " ScreenShot_ " + DateTime.Now.ToString( " yyyy-MM-dd_hh:mm:ss " );
  library.SavePicture(filename, stream);
  stream.Close();
}

  8、让我们的phone 7 震动:

   
   
   
   
VibrateController vib = VibrateController.Default;
vib.Start(TimeSpan.FromMilliseconds(
100 ));

  9、WPF的资源的资源包括静态资源(StaticResource)和动态资源(DynamicResource),他们之间的区别是:

  静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。

  10、在windows phone 7中我们只需关注静态资源(StaticResource)

你可能感兴趣的:(Windows,Phone,7)