Root element is missing when using DataContract

I’ve been using DataContract to serialize objects into IsolatedStorage when I received a "Root element is missing" error when trying to use ReadObject. Turn out it was a simple case of the file existing but with nothing in it, doh.
 

DataContract missing a reference?

An easy way to serialize objects in Silverlight is to use DataContract and DataMember attributes to decorate the objects/properties you want to serialize. One little gotcha is that by default you can add the System.Runtime.Serializable namespace fine but DataContract doesn’t show as useable. You have to add a reference to the System.Runtime.Serializable library to get those features.

Problem playing wav sounds in Silverlight

I was playing around with sounds and Silverlight but kept getting;
—————————
Error
—————————
A Runtime Error has occurred.
Do you wish to Debug?
Line: 433
Error: Sys.InvalidOperationException: MediaError error #4001 in control ‘Xaml1’: AG_E_NETWORK_ERROR
—————————
Yes   No  
—————————
A quick look around the internet showed a number of people having trouble using the correct Uri or not using embedded resources correctly, I was sure I was coding it correctly. Then the obvious struck me, it doesn’t support WAVs. Sure enough once I change to WMA the code sprang to life.
 

Problems using a Blend project after upgrading to Silverlight Beta 2

I just ran into an annoying issue after upgrading to Silverlight Beta 2. Time for a story…
Machine had Blend pre-beta 2 and had created a SL application.
Along came beta 2 and Blend was un-installed and the latest version of Blend installed.
Old Blend project was manually copied and pasted (see previous post on Blend and Beta 2) into a new Blend project, built and tested.
.xap and default.html copied over to web server.
When default.html was opened the page displayed the Get Silverlight banner even though the browser had the correct Silverlight. Clicking Get displayed a site saying that the site was using an old version of Silverlight.

So I took the source files from Blend and opened them on a machine with Visual Studio + beta 2 toolkit. This told me that the project needed upgrading (eh and why?) so I let it and built the project. Deployed it to the web site and everything worked fine! Not sure what happened there but I’m suspicious of only having Blend on a machine, I’m not ruling out some manual mess-up but why would the Blend build not raise the problem? Oh well, I”ll put it down to beta fun.

Problems upgrading Silverlight Beta 2 project

Finally had to upgrade one of our development machines to Silverlight 2 Beta 2, and boy did Blend not like it. After installing Beta 2 the project opened in Blend but crashed when you tried to build the project. So uninstalled Blend, installed the latest version and tried again. This time the browser launched but there was an error (from javascript) and an empty page. After some messing around I finally threw my hands in the air and just created a brand new project and copied the assets over. That didn’t work either. The new project had a different name and after trying seemingly everything (I’m not a Blend person) I gave up and created yet another project but this time was careful to give it exactly the same name. Finally the project runs. I thought we could have at least had an upgrade old project, oh well that’s the life of a beta user I guess.
 

Implementation of missing combo control/drop down control

I found Michael Sync blog whilst looking for a Silverlight combo box control, I’ve not tried it yet but it looks promising since it contains the source too (never take a control without the source code!).

A couple of things that don’t (currently) work in Silverlight

I’ve just tried a couple of ‘tricks’ in Silverlight to avoid writing "code-behind" but both failed.

Using XAML triggers:
One mechanism that should work in the final version is the ability to create XAML that responds to an even and can trigger something else, such as a storyboard. This is especially useful for Designers who want to test their designs/animations without having to write procedural code. The XAML is "supported" but simply doesn’t work, you get odd messages about values been out of range. This is the idea anyway…

<Rectangle.Triggers>

<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">

<BeginStoryboard Storyboard="{StaticResouce mystory}"/>

</EventTrigger>

</Rectangle.Triggers>

However, it’s not all bad news, you can use the above technique if you embed the stroyboard rather than using the static resource. However, this isn’t good for a Designer using Blend as currently blend "loses" the storyboard…you can’t win 😉

Embedding code into XAML:
XAML has a special inline code element, <x:Code> that you place procedural code into it. Ok so you need to write the event handler but that’s pretty close to boiler plate code so easy to copy. Plus you don’t have to leave Blend to write it. However, when I tried it Silverlight just didn’t like it and wouldn’t run the page let alone the code.
 

Silverlight full screen, now with limited keyboard support

Since I blogged about the difficulties of not having keyboard support in Silverlight when you’ve gone full-screen I thought I’d better say that with the latest release of Silverlight 2 we can now access the basic keyboard navigation, e.g. tab, arrow keys, space, etc. It’s better than nothing I suppose. I think I’d prefer some form of keyboard overlay that lights up as you type, so for those paranoid about password sniffing attacks the user could see that the application is catching the keyboard messages regardless of the visual state of underlying application. Somehow I doubt that will happen.
 

Silverlight Deep Zoom

I’ve just had my first try of Deep Zoom, the collection of tools as user controls that allow the user to zoom into what seemingly looks like a single image. The idea is that as a developer (or publisher) you can collate a number of images together using the Deep Zone Composer to form one Gestalt style super image. So what can you use it for? Well IMO there are two main uses;
1. A scrollable collage of pictures where you publish just the one Deep Zoom picture and you scroll around zooming into to each picture where each picture still retains the original resolution. The BBC Radio 1 and Hard Rock cafe both have examples of this. Is it useful, hmm, well it’s a way of showing pictures and I’m sure there are benefits in reducing the bandwith of high-res pictures that the user never looks at but for me it’s not very exciting.
2. Continuous zooming. This is the one that interested me. The ability to scroll on an area and keep scrolling to the tinest detail sounds very useful. Not unlike the way Google/Live maps work…or for me like Bladerunner!
 
To try Deep Zoom out I first downloaded the relvant tools from www.Silverlight.net and used the Deep Zoom Composer to create my image. I wanted the continuous zoom idea so to do that you need to take a series of picture of something, each time zooming in a specific area(s). Then in the composer you resize and position them on top of each other, carefully lining them up. This is a difficult process on two counts, 1st taking the pictures is tricky…you need good lighting and very careful positioning of the camera. The 2nd problem is the composer doesn’t (seem) to allow you to set an opacity on the picture you’re trying to place. That makes it difficult to line the two pictures up correctly. Once you’re satisfied with the picture you export it into a Silverlight project. In my case this project didn’t work, just got the dreaded catistrophic exception, or to those who develop Silverlight, the standard XAML exception. I followed the instructions in http://www.codeproject.com/KB/silverlight/DeepZoom.aspx and created a separate Silverlight project and copied the assets over. That worked, so what is the result? Well yes it works, the user can zoom into the pictures detail pretty easily, but is it any use outside a bit of fun? The problems of creating/composing the image makes it difficult to recommend for everyday use, I think for one-off imagery it is powerful, launching a new car maybe, perhaps medical applications but these are quite specialist. I think coupled with an excellent image detection/mapping/stitching library then it would become a powerful tool for applications that display images.
 
 

Silverlight Digg search example

I’ve decided to follow Scott Guthrie’s Silverlight example for creating a Digg Search application. It’s a nice example covering a number of topics, however…there are a few gotcha’s (problems) in it.
  1. The tutorial involves a number of swaping one element type for another, the compiler can get easily confused. You can get weird errors that reference the old element when it is nowhere to be found in the source code. It seems that stepping though the initialize code seems to solve this!??
  2. The data control in the tutorial is called Data:DataGrid, for me it’s my:DataGrid
  3. Part of the tutorial explains how to separate style from structure, however a number of the controls the tutorial uses don’t support styles as resources. However, if you download the sample code you’ll see that Scott has replaced those with more basic versions, for example the tutorial uses a WatermarkTextBox (doesn’t support resource styles) whereas the sample code uses a straight forward TextBox.
  4. The tutorial shows the application displaying images, and indeed it does show how to bind to the thumbnail image. However, neither the tutorial or the sample code actually displays images. The problem seems to be that the Linq query is trying to store an entire Xml element into a string, doesn’t work. So you need to change the Linq query from

    Thumbnail = (string)story.Element("thumbnail"), to Thumbnail = (string)story.Element("thumbnail").Attribute("src"),

  5.   Binding the Hyperlinkbutton directly to the string href of the story class doesn’t work, it needs to be a URI. So add a URI property to the story…

public Uri HrefUri{get{return new Uri(this.HrefLink);}}