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

天气信息

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.69/5 (12投票s)

2004年5月28日

CPOL

2分钟阅读

viewsIcon

76776

downloadIcon

636

天气信息使用 .NET Compact Framework 上的 Web 服务,在 Pocket PC 上显示天气信息。

Sample Image - weatherinfo.gif

引言

天气信息这个项目源于我之前使用的一个天气程序因为时间限制而停止工作,并且开发者也消失了。我开始学习如何在 .NET Compact Framework 上使用 Web 服务,并最终开发出我认为对我来说非常有用的第一个应用程序。

实现

该项目包含一系列类,用于实现 Web 服务调用以及用于离线使用的数据缓存。WeatherInfoMain.cs 包含所有实现用户界面的主要代码。WeatherItem.cs 包含用于存储和序列化 Web 服务响应的信息。WeatherCache.cs 提供了缓存和检索 WeatherItems 的机制,而 Registry.cs 包含用于操作 Pocket PC 注册表的代码。

Web 服务设置

天气信息使用 EJSE 提供的天气数据。(抱歉,对非美国用户来说,此服务仅支持美国。)实际上,我对设置 Web 服务请求和从服务检索数据的方式感到非常惊喜。需要注意的是:Pocket PC 上的代理配置最多只能说是勉强可行,因此该应用程序不支持使用代理。

Pocket PC 注册表

该应用程序使用 PInvoke 调用本机 Pocket PC 注册表处理程序。我使用注册表来存储 Web 服务检索天气信息时使用的 ZIP 码。

滚动窗体

我在应用程序中面临的另一个重大挑战是,在用户显示软键盘和扩展天气预报时开发滚动机制。当显示软键盘时,可视区域会缩小到所选输入法的大小。为了处理这个问题,我将所有数据开发为一系列面板,然后将这些面板放入主选项卡控件中。根据需要显示滚动条,以允许用户查看面板的全部内容。

/// <summary>
/// Handles setting scroll bars and viewable areas
/// when the soft input panel is enabled or disabled
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void inputPanel1_EnabledChanged(object sender, System.EventArgs e)
{
    if(this.inputPanel1.Enabled)
    {
        //subtract SIP height from tab control height
        this.tabControl1.Height= 
          this.ClientRectangle.Height - this.inputPanel1.Bounds.Height;
        this.vScrollBarCurrent.Visible=true;
        this.vScrollBarAbout.Visible=true;
        this.panelAbout.Left=-(this.vScrollBarAbout.Width/2);
    }
    else
    {
        this.tabControl1.Height=this.ClientRectangle.Height;
        this.panelCurrent.Top=0;
        this.vScrollBarCurrent.Value=0;
        this.vScrollBarCurrent.Visible=false;
        this.panelAbout.Top=0;
        this.panelAbout.Left=0;
        this.vScrollBarAbout.Value=0;
        this.vScrollBarAbout.Visible=false;
    }
    int iCurrent=this.panelCurrent.Height-this.tabCurrent.Height;
    this.vScrollBarCurrent.Maximum=iCurrent+((iCurrent/10)/2);
    this.vScrollBarCurrent.LargeChange=iCurrent/10;
    this.vScrollBarCurrent.Height=this.tabCurrent.Height;
    int iExtended=this.panelExtended.Height-this.tabExtended.Height;
    this.vScrollBarExtended.Maximum=iExtended+((iExtended/10)/2);
    this.vScrollBarExtended.LargeChange=iExtended/10;
    this.vScrollBarExtended.Height=this.tabExtended.Height;
    int iAbout=this.panelAbout.Height-this.tabAbout.Height;
    this.vScrollBarAbout.Maximum=iAbout+((iAbout/10)/2);
    this.vScrollBarAbout.LargeChange=iAbout/10;
    this.vScrollBarAbout.Height=this.tabAbout.Height;
}

所需的库

序列化支持需要 Compact Formatter

结论

希望您喜欢使用天气信息。更新版本可以在 这里 找到。

© . All rights reserved.