65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET CF 和 Google API

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.22/5 (29投票s)

2004年5月17日

CPOL

4分钟阅读

viewsIcon

74178

downloadIcon

479

允许从 .NET Compact Framework 应用程序访问 Google API 的示例应用程序

Sample Image - dotNETCF_Google.png

引言

这篇文章主要是为了尝试赢得一台 Pocket PC!我最初向 MSDN 加拿大提交了一篇关于“代码解读”大赛的文章,并赢得了四月份的奖项。那篇文章的标题是“.NET CF 多线程技巧和最佳实践”,重点介绍了使用 Compact Framework 访问 Google API 以及多线程应用程序以避免 UI 锁定。我对原始应用程序做了一些更新,这篇文章将重点介绍三个新项目

  1. 支持 Square(240x240) 或 Rectangular(240x320) 的 Windows Mobile 2003 SE 的横向和纵向模式
  2. 创建用于设置和关于屏幕的“浮动”窗口

背景

提供的源代码是我为 MSDN 加拿大举办的“代码解读”大赛开发的原始代码的延续,主要涉及 Compact Framework 应用程序的多线程处理。要阅读该文章,请在此处点击。另外值得注意的是,.NET CF 的 Google API 使用了 OpenNETCF.org Smart Device Framework (SDF)。这些文件包含在演示项目中,但不包含在源代码项目中。要下载这些文件,请访问 http://www.opennetcf.org/sdf。使用到的主要 SDF 库是 OpenNETCF.Windows.Forms.HTMLViewer(用于显示 Google 搜索结果)和 OpenNETCF.Configuration.ConfigurationSettings。OpenNETCF.org 库对于任何正在为 Compact Framework 开发的开发人员来说都是一个很棒的补充。如果您目前不使用它,我建议您下载 SDF http://www.opennetcf.org/ 并开始使用。SDF 增加了 Compact Framework V1 中未包含的许多功能,最重要的是源代码也包含了!!

要运行代码,您必须在 http://www.google.com/api 注册并获取一个密钥。获取密钥后,您必须在应用程序的设置中输入它,然后您就可以进行搜索了。Google API 仍处于测试阶段,我不知道将来是否会收取搜索费用,但我相信他们会通知我们。

纵向或横向

我将首先介绍支持 Windows Mobile 2003 SE 的横向和纵向模式。要利用不同的屏幕布局,您必须通过代码布局所有控件并处理窗体的 Resize 事件。如果不这样做,如果任何控件超出屏幕边界,滚动条将自动添加到您的窗体中。

Portrait

图 1:纵向模式

Landscape

图 2:横向模式

下面的示例展示了示例应用程序如何根据屏幕大小调整控件。

//
// Wiring up the event to handle resize

//

this.Resize+=new EventHandler(Form1_Resize);
 
//
// Event handler for Resize
// 
private void Form1_Resize(object sender, EventArgs e) 
{ 
   this.ResizeForm(); 
}
 
//
//Adjust the controls approprialty
// 
private void ResizeForm() 
{
   //Setup the buttons for the screen 
   this.btnSearch.Left = this.Width - this.btnSearch.Width - 1; 
 
   //Resize the buttons 
   this.btnNext.Left = this.Width-this.btnNext.Width - 1; 
   this.btnNext.Top = this.Height-this.btnNext.Height-1; 
   this.btnPrevious.Top = this.btnNext.Top;  
   this.btnPrevious.Left = 1;
   this.lblCount.Top = this.btnNext.Top;
   
   //resize the html screen 
   this.htmlGoogleResults.Left = 1; 
   this.htmlGoogleResults.Top = this.btnSearch.Top + this.btnSearch.Height + 1; 
   this.htmlGoogleResults.Height = this.Height- htmlGoogleResults.Top - 
                                  btnNext.Height-2; 
   this.htmlGoogleResults.Width = this.Width -2; 

 
   //Resize the search bar 
   this.textBox1.Width = this.Width-this.btnSearch.Width -3; 

 
   //Resize the result lable 
   this.lblCount.Left = btnPrevious.Left+btnPrevious.Width + 2; 
   this.lblCount.Width = this.Width - this.lblCount.Left - 
                         this.btnPrevious.Width -2; 
  
   //Center the logo 
   this.pictureBox1.Left = (this.Width/2)-(this.pictureBox1.Width/2); 
   this.pictureBox1.Top = (this.Height/2)-(this.pictureBox1.Height/2); 
  
   //Center the progress bar 
   this.progressBar1.Width = (this.Width*75)/100; 
   this.progressBar1.Left = (this.Width/2)-(this.progressBar1.Width/2); 
   this.progressBar1.Top = this.pictureBox1.Top+this.pictureBox1.Height+2; 
}

Visual Studio 2005 和 Compact Framework V2 将支持控件的停靠和锚定。希望届时,当用户决定切换屏幕方向时,您不必手动调整控件。要了解 Windows Mobile 2003 SE 的新功能,请访问 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwm2k3/html/whatsnew2003se.asp

“浮动”窗口

有很多关于创建“浮动”窗口的信息,所以我不会详细介绍。基本上,您需要将 Form 的以下属性设置为 false

  • ControlBox
  • MinimizeBox
  • MaximizeBox

Landscape

您可能还想做的另一件事是将您的窗体居中显示。以下代码将在设备上居中显示窗体

//
// Center the screen
//
Rectangle _screen = Screen.PrimaryScreen.WorkingArea; 
this.Location =new Point(((this._screen.Width - this.Width) / 2),
                          ((this._screen.Height - this.Height) / 2));
         
        

结论

如我上面所说,我写这篇文章的主要原因是为了尝试赢得一台 Pocket PC,但我也觉得这是一个很酷的应用程序。它有很大的用处吗?我不知道,您告诉我。我开始写它是因为 Google 搜索 API 可用,并且我认为从 Pocket PC 访问它们会很酷。该应用程序的作用是通过一些针对 Compact Framework 的开发技术,希望它能对一些人有所帮助。别忘了查看第一篇文章。此外,Google API 还公开了“缓存请求”和“拼写请求”。我没有将它们包含在此应用程序中,也不打算这样做(除非我真的很无聊!),但如果有人将它们包含在此应用程序中,请告诉我。我认为拼写请求在某个应用程序中会很有用。如果您有任何问题或意见,可以通过 info@markarteaga.com 联系我,或在下方添加评论。

© . All rights reserved.