WPF模拟Windows7气象组件

WPF模拟Windows7气象组件

       本篇将利用Weather Reader User Control(WRUC) 模拟Windows Gadgets 中的气象组件 WRUC 控件是使用C#和 WPF 制作的 首先下载WRUC 源代码 其实该控件已经实现了所有天气预报的功能 只需将DLL 库导入到其他项目使用即可

      
  WRUC 更新


       在使用WRUC 控件之前先了解一下它的工作模式 并完善其中的一些功能 通过源码可以看出控件是通过 服务获取某地天气数据(XML格式) 再将XML 读取出来用WPF 呈现给用户 下图为WRUC 默认的界面样式 可以看到控件默认是Redmond 地区的天气 虽然可以设置为其他地区 但还是希望程序运行时就是本地天气数据 同时还想在当日气温下方添加当日温度范围 并将所有温度改为摄氏度

  

  在WRUC 项目中可以看到Settings settings 配置文件 打开后里面即为Redmond 地区信息 将其修改为本地信息 这里我全部改为Beijing

  

  这样控件默认地区就是Beijing了 并且DegreeType 可以将温度默认调整为摄氏度

  

  地区和温度类型设置完成后 下面来添加当日气温范围数据 也就是获取当日最低和最高气温 在MsnWeatherDataProvider 类中当前气温信息是通过WeatherPoint 类完成的 所以先在WeatherPoint cs 中添加最高温度和最低温度以及温度范围

private double highTemperature;

public double HighTemperature

{

    get { return Math Floor(highTemperature); }

    set { highTemperature = value; }

}

private double lowTemperature;

public double LowTemperature

{

    get { return Math Floor(lowTemperature); }

    set { lowTemperature = value; }

}

public string TemperatureRangeString

{

    get { return LowTemperature ToString() + ° + HighTemperature ToString() + ° ; }

}

  回到MsnWeatherDataProvider 类 GetLatestWeatherReport 方法通过XmlTextReader 读取气象服务返回的XML 代码获得控件中所需的气温数据 那当日气温范围的数据在什么地方呢?这就需要看一看XML 代码返回的是什么内容了

  通过浏览x?src=vista&wealocations=wc:CHXX 获得下面XML 代码 可见当日最低与最高气温数据就在第一个forecast 的low 和high 参数中 因为所有forecast 的数据早已被GetLatestWeatherReport 保存到WeatherReport 类的Forecast中 下面只需从Forecast 吊起就OK了

<?xml version= ?>

<weatherdata>

    <weather weatherlocationcode= wc:CHXX weatherlocationname= Beijing CHN               zipcode= encodedlocationname= Beijing% c+CHN               url=x?wealocations=wc:CHXX &q=Beijing% c+CHN              imagerelativeurl= us/ degreetype= F provider= Foreca               attribution= Data provided by Foreca attribution = © Foreca               lat= long= timezone= alert= >

        <current temperature= skycode= skytext= Clear date= day= Tuesday                   shortday= Tue observationtime= : : observationpoint= Beijing                   feelslike= humidity= windspeed= winddisplay= mph NNW />

        <forecast low= high= skycodeday= skytextday= Fair date=                    day= Tuesday shortday= Tue precip= />

        <forecast low= high= skycodeday= skytextday= Fair date=                    day= Wednesday shortday= Wed precip= />

        <forecast low= high= skycodeday= skytextday= Partly Cloudy date=                    day= Thursday shortday= Thu precip= />

        <forecast low= high= skycodeday= skytextday= Cloudy date=                    day= Friday shortday= Fri precip= />

        <forecast low= high= skycodeday= skytextday= Clear date=                    day= Saturday shortday= Sat precip= />

        <toolbar timewindow= minversion= />

    </weather>

</weatherdata>

  从GetLatestWeatherReport 得知forecast(预报) 数据存在result Forecast 中

result Forecast Add(forecast);

  current(当日)数据存在result LatestWeather 中

result LatestWeather = current;

  那么只需在result Location = location; 前添加下面两行代码即可 因为当日forecase 是第一组数据 所以Index 应该为

result LatestWeather HighTemperature = result Forecast[ ] HighTemperature;

result LatestWeather LowTemperature = result Forecast[ ] LowTemperature;

result Location = location;

  最后在WeatherReaderUI xaml 的MainGrid 中将TemperatureRangeString 显示在TextBlock 就可以了

<TextBlock Foreground= {Binding Path=TextColor} HorizontalAlignment= Right

           Margin= VerticalAlignment= Top Width= Auto Height= Auto

           Text= {Binding Path=LatestWeather TemperatureRangeString Mode=OneWay}

           TextWrapping= Wrap x:Name= currentTempRangeText

           RenderTransformOrigin= >

    <TextBlock RenderTransform>

        <TransformGroup>

            <ScaleTransform ScaleX= ScaleY= />

            <SkewTransform AngleX= AngleY= />

            <RotateTransform Angle= />

            <TranslateTransform X= Y= />

        </TransformGroup>

    </TextBlock RenderTransform>

</TextBlock>

    制作WPF 程序

  重新编译WRUC 后得到新的WeatherReaderMVC dll 有了它接下来的工作就简单了 新建项目将DLL 加入其中 在MainWindow xaml 中加入WeatherReaderMVC 命名空间 在通过WindowStyle Background AllowsTransparency ResizeMode 将WPF 设置为透明 无边框 不可修改尺寸的窗口模式 如下代码通过 添加上面刚刚修改好的WRUC 控件 由于边框被取消所以添加<Image>控件用来关闭应用程序

  查看其它地区天气情况 到了晚上太阳图标也会变化

  

  由于WRUC 使用的是Windows 气象组件的图像资源 所以从外观上看两者大致相同

   

<Window x:Class= WpfWeatherReporter MainWindow

        xmlns=

        xmlns:x=

        xmlns:w= clr namespace:WeatherReaderMVC;assembly=WeatherReaderMVC

        Title= MainWindow Height= Width= WindowStyle= None

        Background= Transparent AllowsTransparency= True ResizeMode= NoResize

        Icon= /WpfWeatherReporter;component/Images/Sunny ico          WindowStartupLocation= CenterScreen >

    <Grid>

        <w:WeatherReaderUI MouseLeftButtonDown= WeatherReaderUI_MouseLeftButtonDown />

        <Image Source= /WpfWeatherReporter;component/Images/Close png                 MouseLeftButtonDown= Image_MouseLeftButtonDown

               Width= Height= Margin= ToolTip= Close />

    </Grid>

</Window>

  

  WeatherReaderUI_MouseLeftButtonDown 和Image_MouseLeftButtonDown 事件分别用于移动窗口和关闭窗口

private void WeatherReaderUI_MouseLeftButtonDown(object sender MouseButtonEventArgs e)

{

    this DragMove();

}

private void Image_MouseLeftButtonDown(object sender MouseButtonEventArgs e)

{

    this Close();

}

  至此 所有的工作已完成 下图为程序运行后的初始和迷你界面  

WPF模拟Windows7气象组件

相关文章:

你感兴趣的文章:

标签云: