Writing a (Completely Useless) Windows 7 Gadget using Silverlight

At its simplest form a Windows gadget is comprised of a .html file and a .xml file. Think of it as a mini-web site, with the .html file representing the site’s visual components and the .xml file the gadget config or definition. Of course, as a web site, most gadgets are going to have the usual suspects: css, JavaScript, image files, and—best of all—Silverlight.

The purpose of this post is to allow me to do some research on what Windows gadgets are, how to write them, and, most importantly, how to leverage Silverlight to make gadget development easier and more fun.

What is a Windows Gadget?

When I ask this question, I mean what is a Windows gadget, really? I already know what they are, but what’s inside the .gadget file? It’s easy to see. Gadget files, which use the .gadget file extension, are just compressed .zip files (much like a .xap in Silverlight). If you change the .gadget extension to .zip, you can then view the files inside just like any other ZIP archive.

Conversely, when you install a .gadget file, its contents are uncompressed to the following location:

C:Users<username>AppDataLocalMicrosoftWindows SidebarGadgets

I’ll take a quick look at The Weather Channel gadget (one of the best, IMO):

image

As you can see, it has all the basic components of a standard web site. There are multiple .html files because this gadget, like many others, has a variety of views, each one based on the different gadget states: docked, undocked, fly-out (floating).

The XML Definition File

When you get down to it, the XML definition/config file (not really sure what to call it) is the only thing new. The rest of the gadget is stuff we’ve seen before.

Here’s the generic layout of the .xml definition file, stolen borrowed from Donavon West’s Build Your Own Windows Vista Sidebar Gadget article on MSDN:

   1: <?xml version=”1.0encoding=”utf-8” ?>
   2: <gadget>
   3:   <name>Gadget Name Here</name>
   4:   <namespace>YourCompanyNameHere</namespace>
   5:   <version>1.0.0.0</version>
   6:   <author name=”Company Name Here>
   7:     <info url=”http://contoso.comtext=”Visit our Web site/>
   8:     <logo src=”logo.png/>
   9:   </author>
  10:   <copyright>&#0169; 2007</copyright>
  11:   <description>your gadget description</description>
  12:   <icons>
  13:     <icon width=”64height=”64src=”icon.png/>
  14:   </icons>
  15:   <hosts>
  16:     <host name=”sidebar>
  17:       <base type=”HTMLapiVersion=”1.0.0src=”gadget.html/>
  18:       <permissions>full</permissions>
  19:       <platform minPlatformVersion=”0.3/>
  20:     </host>
  21:   </hosts>
  22: </gadget>
View Plain

Donavon says this about the XML content:

Most of the elements in the definition file are used for displaying the gadget in the gallery. The one truly functional element is the src attribute of the base element—this points to the HTML file that will kickstart the gadget. I make it a practice to name this file gadget.html, but any valid filename will do.

So it’s the XML file that acts as a bridge between the gadget hosting app (still sidebar.exe in Windows 7) and the gadget itself.

Creating a Windows Gadget using Silverlight

Creating a gadget that uses Silverlight is surprisingly easy.

1.) Start with a basic Silverlight Application project:

image

I’ll call mine SilverlightGadget.

I’ll host the control in an ASP.NET Web Site and use Silverlight 3:

image

 

Once the project is created, you can delete the SilverlightGadgetTestPage.aspx from the web site (we aren’t going to use it). I also renamed the SilverlightGadgetTestPage.html file to just gadget.html as Donavon recommends.

2.) Next, add a new .xml file to the web site. I called this file gadget.xml to match the naming of the HTML file. Copy the generic <gadget> contents shown above into this new file. You can update information in the file as you see fit. For purposes of this demo, I’ll just leave it the way it is except for the gadget’s <name> which I’ll call SilverlightGadget. The really important line is this file is:

<base type=”HTML” apiVersion=”1.0.0″ src=”gadget.html” />

Make sure “src” points to your HTML file.

3.) In gadget.html, we have a couple of changes to make:

a. Make the “html, body” style read “height: 80px; width: 130px;”

   1: html, body {
   2:     height: 80px;
   3:     width: 130px;
   4: }
View Plain

Make sure to remove the overflow style. It was causing me problems, specifically causing horizontal and vertical scrollbars to display.

The height value was arbitrary; you’ll want to adjust based on the content of your gadget. But the width of 130px was a maximum under Vista for a docked gadget. In Windows 7, however, this doesn’t seem to apply as I was able to make the gadget as wide as I wanted. Still, all of the other docked gadgets that I have do not exceed this width, so I went with the standard 130px.

b. Add the following param to the list of object <param> tags:

<param name=”windowless” value=”true” />

This seemed to have the effect of enabling the gadget handles when you mouseover the gadget.

4.) Now, in the XAML, I’ll remove the height and width attributes from the UserControl and add a simple button inside the default grid. This gives me:

   1: <UserControl x:Class="SilverlightGadget.MainPage"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   6:     mc:Ignorable="d">
   7:  
   8:     <Grid x:Name="LayoutRoot" Background="RosyBrown">
   9:         <Button Background="AliceBlue" Width="70" Height="30" Content="Click"  />
  10:     </Grid>
  11: </UserControl>
View Plain

Yes, I do realize this is a completely useless Silverlight control, but, for now, I’m just trying to demonstrate how to build the gadget by laying some groundwork. Perhaps in a future post I’ll share some more useful Silverlight controls to host inside a gadget.

That should be it for the project/code changes. Now, let’s install the gadget and see just how useless my one button control is.

Installing the Silverlight Gadget

As mentioned above, a gadget is a just a zip file with the extension .gadget. The easiest way to create a .gadget file is therefore to just use your favorite zip program. Mine is 7za, which I use inside a batch file so that I just double-click or run the batch file from the command line in order to both create and install the gadget.

Here’s the sequence of commands (I run this from the project root; the batch file sits alongside the .sln file):

   1: cd SilverlightGadget.Web
   2: del *.gadget
   3: 7za a -r silverlightgadget.zip *
   4: rename *.zip *.gadget
   5: silverlightgadget.gadget
   6: cd ..
View Plain

I’m just zipping up the whole web site, including the .xap and even the web.config, which isn’t needed but this is just demo code so who cares. 😉

Installing the gadget is as easy as double-clicking the .gadget file or typing the name at the command line and hitting enter. You’ll be prompted to install the gadget. Click “Install” and prepared to be impressed:

image

Here it is alongside some other gadgets for better context:

image