This article uses weather Reader user control (wruc) to simulate meteorological components in Windows 7 gadgets. The wruc control is made using C # And WPF. First, download the wruc source code. In fact, the control has implemented all the weather forecast functions. You only need to import the dll library to other projects for use.
Wruc update
Before using the wruc control, first understand its working mode and improve some of its functions. Through the source code can be seen that the control is through the http://weather.service.msn.com service to obtain a certain weather data (XML format), and then read the XML to present to the user with WPF. It is the default Interface style of wruc. You can see that the control defaults to the weather in the Redmond area. Although it can be set to another area, you still wantProgramLocal weather data is used during running. At the same time, you also want to add the temperature range of the day below the temperature of the day, and change all the temperature to degrees Celsius.
In the wruc project, you can see the settings1.settings configuration file, which is the Redmond region information, and change it to local information. Here I change all to Beijing.
In this way, the default region of the control is Beijing, and degreetype can adjust the temperature to degrees Celsius by default.
After the region and temperature type are set, add the temperature range data for the current day, that is, obtain the minimum and maximum temperatures for the current day. In the msnweatherdataprovider class, the current temperature information is completed through the weatherpoint class. Therefore, add the maximum temperature and minimum temperature and the temperature range in 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 () + "°" ;}}
Return to the msnweatherdataprovider class. The getlatestweatherreport method reads the XML returned by the Meteorological Service through xmltextreader.CodeObtain the temperature data required in the control. Where is the temperature range of the day? This requires you to take a look at the content returned by the XML code.
By browsing http://weather.service.msn.com/data.aspx? Src = Vista & wealocations = WC: chxx0008 get the following XML code. The minimum and maximum temperature data for the current day is in the low and high parameters of the first forecast. Because all the forecast data has been saved to the forecast of the weatherreport class by getlatestweatherreport, you only need to Poll It From the forecast.
<? XML version = "1.0"?> < Weatherdata > < Weather Weatherlocationcode = "WC: chxx0008" Weatherlocationname = "Beijing, CHN"
Zipcode = "" Encodedlocationname = "Beijing % 2C + CHN"
URL = Http://weather.msn.com/local.aspx? Wealocations = WC: chxx0008 & Q = Beijing % 2C + CHN
Imagerelativeurl = "Http://blst.msn.com/as/wea3/ I /en-us" Degreetype = "F" Provider = "Foreca"
Attribution = "Data provided by foreca" Attribution2 = "Foreca"
Lat = "39.9125748" Long = "116.3889847" Timezone = "8" Alert = ""> < Current Temperature = "86" Skycode = "32" Skytext = "Clear" Date = "2010-07-20" Day = "Tuesday"
Today = "Tue" Observationtime = "12:00:00" Observationpoint = "Beijing"
Feelslike = "88" Humidity = "52" Windspeed = "7" Winddisplay = "7 mph NNW"/> < Forecast Low = "74" High = "90" Skycodeday = "34" Skytextday = "Fair" Date = "2010-07-20"
Day = "Tuesday" Today = "Tue" Precip = "10"/> < Forecast Low = "77" High = "93" Skycodeday = "34" Skytextday = "Fair" Date = "2010-07-21"
Day = "Wednesday" Today = "Wed" Precip = "10"/> < Forecast Low = "78" High = "93" Skycodeday = "30" Skytextday = "Partly cloudy" Date = "2010-07-22"
Day = "Thursday" Today = "Thu" Precip = "30"/> < Forecast Low = "77" High = "95" Skycodeday = "26" Skytextday = "Cloudy" Date = "2010-07-23"
Day = "Friday" Today = "Fri" Precip = "35"/> < Forecast Low = "77" High = "93" Skycodeday = "32" Skytextday = "Clear" Date = "2010-07-24"
Day = "Saturday" Today = "Sat" Precip = "35"/> < Toolbar Timewindow = "60" Minversion = "1.0.1965.0"/> </ Weather > </ Weatherdata >
We learned from getlatestweatherreport that the forecast (forecast) data exists in result. forecast.
Result. forecast. Add (forecast );
Current (current day) data exists in result. latestweather.
Result. latestweather = current;
You only need to add the following two lines of code before result. Location = location; Because forecase is the first group of data on the current day, the index should be 0.
Result. latestweather. hightemperature = result. Forecast [0]. hightemperature; Result. latestweather. lowtemperature = result. Forecast [0]. lowtemperature; Result. Location = location;
Finally, display temperaturerangestring in textblock in maingrid of weatherreaderui. XAML.
< 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 = "0.5, 0.5"> < Textblock. rendertransform > < Transformgroup > < Scaletransform Scalex = "1" Scaley = "1"/> < Skewtransform Anglex = "0" Angley = "0"/> < Rotatetransform Angle = "0"/> < Translatetransform X = "0" Y = "0"/> </ Transformgroup > </ Textblock. rendertransform > </ Textblock >
Create a WPF Program
Re-compile wruc and get the new weatherreadermvc. dll. With this, the next work will be simple. Add the DLL to the new project. Add the weatherreadermvc namespace to mainwindow. XAML, and set WPF to a transparent, borderless, and unmodifiable window mode through windowstyle, background, allowstransparency, and resizemode. The following code adds the modified wruc control through <W: weatherreaderui>. Because the border is canceled, the <image> control is added to close the application.
< Window X : Class = "Wpfweatherreporter. mainwindow" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns : X = "Http://schemas.microsoft.com/winfx/2006/xaml" Xmlns : W = "CLR-namespace: weatherreadermvc; Assembly = weatherreadermvc" Title = "Mainwindow" Height = "250" Width = "300" 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 = "19" Height = "19" Margin = "253,63, 7,132" Tooltip = "Close"/> </ Grid > </ Window >
The weatherreaderui_mouseleftbuttondown and image_mouseleftbuttondown events are used to move and close windows respectively.
Private voidWeatherreaderui_mouseleftbuttondown (ObjectSender,MousebuttoneventargsE ){This. Dragmove ();}Private voidImage_mouseleftbuttondown (ObjectSender,MousebuttoneventargsE ){This. Close ();}
So far, all the work has been completed. It is the initial and mini interface after the program runs.
Check the weather conditions in other regions and the sun icon changes at night.
Wruc uses the image resources of Windows 7 meteorological components, so the two are roughly the same in appearance.
Source code Download
Wpfweatherreporter.zip
Weatherreader.zip