本篇將利用Weather Reader User Control(WRUC) 模擬Windows
Gadgets 中的氣象組件
WRUC 控件是使用C#和 WPF 制作的
首先下載WRUC 源代碼
其實該控件已經實現了所有天氣預報的功能
只需將DLL 庫導入到其他項目使用即可
WRUC 更新
在使用WRUC 控件之前先了解一下它的工作模式
並完善其中的一些功能
通過源碼可以看出控件是通過 服務獲取某地天氣數據(XML格式)
再將XML 讀取出來用WPF 呈現給用戶
下圖為WRUC 默認的界面樣式
可以看到控件默認是Redmond 地區的天氣
雖然可以設置為其他地區
但還是希望程序運行時就是本地天氣數據
同時還想在當日氣溫下方添加當日溫度范圍
並將所有溫度改為攝氏度

在WRUC 項目中可以看到Settingssettings 配置文件打開後裡面即為Redmond 地區信息將其修改為本地信息這裡我全部改為Beijing

這樣控件默認地區就是Beijing了並且DegreeType 可以將溫度默認調整為攝氏度

地區和溫度類型設置完成後下面來添加當日氣溫范圍數據也就是獲取當日最低和最高氣溫在MsnWeatherDataProvider 類中當前氣溫信息是通過WeatherPoint 類完成的所以先在WeatherPointcs 中添加最高溫度和最低溫度以及溫度范圍
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(預報) 數據存在resultForecast 中
result
Forecast
Add(forecast);
current(當日)數據存在resultLatestWeather 中
result
LatestWeather = current;
那麼只需在resultLocation = location; 前添加下面兩行代碼即可因為當日forecase 是第一組數據所以Index 應該為
result
LatestWeather
HighTemperature = result
Forecast[
]
HighTemperature;
result
LatestWeather
LowTemperature = result
Forecast[
]
LowTemperature;
result
Location = location;
最後在WeatherReaderUIxaml 的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 後得到新的WeatherReaderMVCdll有了它接下來的工作就簡單了新建項目將DLL 加入其中在MainWindowxaml 中加入WeatherReaderMVC 命名空間在通過WindowStyleBackgroundAllowsTransparencyResizeMode 將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();
}
至此所有的工作已完成 下圖為程序運行後的初始和迷你界面
From:http://tw.wingwit.com/Article/os/xtgl/201311/8952.html