Recently I posted some information about hosting a Silverlight 2.0 application on a Windows CE 6.0 based host – This was really straight forward, I built a (very) simple Silverlight 2.0 application in Expression Blend, this generates a .XAP file, I ran the Silverlight application from Expression Blend which launches a test page for the application. I then used IE to view | source and captured the HTML source to act as the host page on my CE 6.0 device. Adding the HTTPD Web Server to my CE 6.0 image, and copying the HTML and XAP files to \Windows\www\wwwpub was all that was needed to host the Silverlight 2.0 application on CE 6.0 and then view the application in my desktop browser.

Hosting the XAP/HTML was easy, but what can I actually do with the hosted Silverlight application? – The application is running in the context of my desktop browser, so doesn’t really know anything about my CE 6.0 device that is simply a bit-bucket to store the HTML and XAP files.

Ideally we need to get the device name or IP address of the CE 6.0 host from within the Silverlight application – if we have the IP address or device name then we can start calling exposed SOAP or REST functions on the CE 6.0 device.

I decided to create a new, simple Silverlight 2.0 application – Here’s the application UI in Expression Blend – it’s a very simple application that has a textBox and a Button.

ExpressionBlend_App

The Silverlight application has two interesting files – app.xaml.cs and page.xaml.cs – we will need to make modifications to both files to get the host name/address, and then do something useful with the address in the application.

Step 1 – In Visual Studio (I’m using VS 2010, but this would work just as well in VS 2008) open the Silverlight 2.0 project, and expand the app.xaml.cs file.

The default OnStartup method looks like this…

   1: private void OnStartup(object sender, StartupEventArgs e) 
   2: {
   3:     // Load the main control here
   4:     this.RootVisual = new Page();
   5: }

We need to change the OnStartup function in two ways.

  1. Get the host name/address
  2. Pass the host name/address to the new page

The contents of the modified OnStartup should look like this.

   1: private void OnStartup(object sender, StartupEventArgs e) 
   2: {
   3:     // Load the main control here
   4:     string HostAdr = this.Host.Source.OriginalString.Substring(0, this.Host.Source.OriginalString.Length - this.Host.Source.LocalPath.Length);
   5:     this.RootVisual = new Page(HostAdr);
   6: }

Note that when we “new up” the page we’re passing the host address – this now requires that we make a change to the page.xaml.cs file.

I’ve created a variable to store the host address (private string HostAddress), you can see the default constructor (public Page()) which does nothing, I’ve added a new constructor that takes the host name/address from app.xaml.cs – The code for the new constructor stores the host name/address and then sets the textBox contents to be the address (so we can see the host address).

   1: private string HostAddress;
   2:  
   3: public Page()
   4: {
   5:     // Required to initialize variables
   6: }
   7:  
   8: public Page(string HostAddr)
   9: {
  10:     HostAddress = HostAddr;
  11:     InitializeComponent();
  12:     textBox2.Text = HostAddress;
  13: }

I used CEFileWiz to create a component for the CE 6.0 catalog that contained the sample HTML file (I called this pFrame.html), and the sample XAP file (SilverlightApplication4.xap), I mapped both files to the \windows\www\wwwpub folder.

My CE 6.0 operating system image contains the HTTP Web Server (SYSGEN_HTTPD) and the sample files.

I can now boot my CE 6.0 image, browse to the IP address of the device (I’m displaying the IP address on my device UI) and that will bring up the default Windows CE “HTTPD Web Server is running” message. Appending a \pFrame.html to the URL will display the Silverlight 2.0 application which includes the web server host IP address (see below).

IE_SLAPP

Now that I have the device address I can start working on calling exposed SOAP or REST services. My Silverlight 2.0 remote UI for CE 6.0 is coming together…

Look out for a follow up post on calling exposed SOAP/REST services on the CE 6.0 device.

- Mike