WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Pushing Data to the Gadget
The next thing you need to do is to push some data to the SideShow device. Code the "Push Data" button Click event code as shown in
Listing 1.
 | |
Figure 16. Push Data Output: Here's how the simulator looks when you click the "Push Data" button. |
SideShow devices display information formatted in SCF (Simple Content Format), a simple XML format. Using the ScfSideShowGadget class, you can obtain the current gadget using its GUID and then add content to it in the SCF format. The first thing
Listing 1 adds to the gadget is the Glance Data, which is displayed on the first page of the SideShow device. The code then adds three menus to the gadget, and for each menu item, adds a text page. Press F5 to test the gadget. Click the "Push Data" button and observe the output on the SideShow Simulator.
Figure 16 shows the output.
You now should have a good idea of how a basic SideShow gadget works!
Building the RSS Aggregator
The remainder of this article shows you how to extend the simple SideShow gadget into an RSS Aggregator so that you can always check for the latest news on your SideShow device without needing to keep your computer turned on all the time. Using the same project you created earlier, add a resource file to your project, accepting the default name of
Resource1.resx (see
Figure 17). You will use the resource file to add some images to your project.
 | |
Figure 17. Adding a Resource File: From your project's context menu, click "Add New Items " and select "Resources File." |
|
 | |
Figure 18. Adding Resources: The figure shows the process of adding resources to a project. |
|
|
After adding the resource file to the project, double-click on
Resource1.resx in Solution Explorer and add an existing file (see
Figure 18).
 | |
Figure 19. Adding Image Resources: Add the .jpg and .ico files shown in the image to the sample project. |
Add the three
.jpg files and the
.ico file shown in
Figure 19 (these images are in the
downloadable code).
You'll need to modify the code to add an icon for this SideShow gadget by modifying the
Register() method as shown below:
Private Sub btnRegister_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnRegister.Click
'---register the gadget---
GadgetRegistration.Register( _
False, _
_GadgetID, _
ScfSideShowGadget.ScfEndpointId, _
"RSS Aggregator Gadget", _
Nothing, _
"C:\Users\Wei-Meng Lee\Documents\Visual Studio " & _
"2005\Projects\RSSGadget\RSSGadget\Resources\rss.ico", _
False, _
GadgetCachePolicies.KeepNewest, _
Nothing)
End Sub
 | |
Figure 20. Gadget with Icon: Here's how the gadget looks in the simulator after adding the custom icon. |
To force the icon to display on the SideShow device, you need to register the gadget again and then push the data to the device.
Figure 20 shows the icon displayed on the SideShow device.
You need to import the following additional namespaces so you can use the appropriate libraries to download RSS documents and manipulate them:
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Xml.XPath
Declare a
lastID member variable:
Public Class Form1
'---You can get a GUID from http://kruithof.xs4all.nl/uuid/uuidgen---
Private _GadgetID As Guid = _
New Guid("c1c2c0e0-b277-11db-abbd-0800200c9a66")
'---last menu ID used---
Private lastID As Integer
Each menu item in the SideShow gadget has an ID. You use this ID to uniquely reference each menu itemhence the need for the
lastID variable.
To extract the contents from the RSS feeds, define an
ExtractFeed() function as follows:
'---extract Title and Description for each post in a feed---
Private Function ExtractFeed(ByVal feedURL As String) As String
'---download the RSS document---
Dim ftpReq As Net.WebRequest = WebRequest.Create(feedURL)
Dim ftpResp As Net.WebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = ftpResp.GetResponseStream
Dim reader As StreamReader
reader = New StreamReader(ftpRespStream,
System.Text.Encoding.UTF8)
'---load the RSS document into an XMLDocument object---
Dim xml As New XmlDocument
xml.Load(reader)
'---select all <rss><channel><item> elements---
Dim nodes As XmlNodeList = xml.SelectNodes("rss/channel/item")
Dim result As String = String.Empty
For Each node As XmlNode In nodes
'---select each post's <title> and <description> elements---
result += node.SelectSingleNode("title").InnerText & Chr(3)
result += node.SelectSingleNode("description").InnerText & _
Chr(12)
Next
Return result
End Function
The
ExtractFeed() function downloads the RSS feed from a site and then extracts the
<title> and
<description> pair of elements from each post and saves them in a string, separating each post with ASCII character 12 and each title and each description by ASCII character 3.
The
GetPosts() subroutine in
Listing 2 calls the
ExtractFeed() function to get the concatenated string, and then processes the result, adding each post as menus and text in the SideShow gadget:
Finally, modify the code for the Push Data button so it adds the feeds to the gadget:
Private Sub btnPushData_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnPushData.Click
Dim gadget As ScfSideShowGadget = _
New ScfSideShowGadget(_GadgetID)
With gadget
'---main page---
.AddGlanceContent("RSS feeds updated on " & vbCrLf & _
Now.ToString)
'---add menu items under the Feeds page---
.AddContent( _
Scf.Menu( _
ScfSideShowGadget.HomePageContentId, _
"Feeds", _
ScfSelectAction.Target, _
Scf.Item(2, "Digg.com"), _
Scf.Item(3, "DevX.com"), _
Scf.Item(4, "MSDN.com")))
 | |
Figure 21. The Completed RSS Aggregator: The series of screenshots shows how the finished RSS Aggregator gadget looks in the simulator. |
lastID = 5 '---2, 3, and 4 are for digg.com,
' devx.com, and MSDN.com---
GetPosts(2, "http://digg.com/rss/index.xml")
GetPosts(3, _
"http://services.devx.com/outgoing/devxfeed.xml")
GetPosts(4,
"http://msdn.microsoft.com/rss.xml")
End With
End Sub
That's it! Press F5 and then click the Push Data button to send the feeds to the SideShow gadget.
Figure 21 shows the output.
Adding Images to Menu Items
 | |
Figure 22. A Better Display: Here's the spiced-up version that displays an image next to each menu item. |
If you observe carefully, you listed each feed as menus under the Feeds page. You can spice things up a little by displaying images next to each menu item. This can be achieved by modifying the code for the "Push Data" button as shown in
Listing 3.
Press F5 to test the application and click the "Push Data" button.
Figure 22 shows the images displayed next to each menu item.
In this article, you have seen the basics of SideShow and how to develop your own SideShow gadgets using the Windows Vista SDK and Visual Studio 2005. As supporting hardware becomes more available, programming SideShow gadgets will become increasingly important--not just for laptops, but for other devices as well.