Category Archives: Silverlight

Silverlight 1.1 – Look before you leap (into LINQ or into WCF)

I spent a lot of time coding in javascript for silverlight 1.0 and am having fun now getting back to managed code with 1.1. I’m even doing it all in C#, but the main reason is because I  have a lot of nearly reusable javascript code and it’s just easier to port that to C#.

However, as usual, I am just diving head first and not doing too much advance research. Two things I learned the hard way:

Silverlight does not yet support LINQ
I spent bunch of time buliding a beatiful LINQ to XML method to build up some XAML by hand. But it was my first time using LINQ to XML to build and XML document, so what I learned in doing that was not a waste by any means.

Silverlight does not yet support WCF
I built a very nice WCF service, with composite types and everything only to learn (when I went to add service reference to my SL client) that it’s not supported yet. There are a few workarounds, [here on the sliverlight forums] and [here in Luis Abreu’s blog], but since this particular code will be for a conference demo and I don’t want to add any extra layers of complexity, I will just go back to ASMX for this one.

LINQ to XML in Silverlight – Not! (Well, not yet…)

Hey, so I’m not a walking encyclopedia.

I thought it would be cool to try to build an xml file from a Silverlight object – namely the StrokeColleciton of an InkPresenter.

I coded it up in what I thought was a very nice way:

   XElement XMLStrokes = new XElement(“StrokeCollection”);
   //create stroke then add to collection element   
   XElement mystroke;
   foreach (Stroke s in mystrokes)
   {
    mystroke = new XElement(“Stroke”,
     new XElement(“Stroke.DrawingAttributes”,
      new XElement(“DrawingAttributes”,
        new XAttribute(“Color”, s.DrawingAttributes.Color),
        new XAttribute(“OutlineColor”, s.DrawingAttributes.OutlineColor),
        new XAttribute(“Width”, s.DrawingAttributes.Width),
        new XAttribute(“Height”, s.DrawingAttributes.Height))));

    //create points separatly then add to mystroke xelement
    XElement myPoints = new XElement(“Stroke.StylusPoints”);
    foreach (StylusPoint sp in s.StylusPoints)
    {
     XElement mypoint = new XElement(“Stylus Point”,
      new XAttribute(“x”, sp.X),
      new XAttribute(“y”, sp.Y));
     myPoints.Add(mypoint);
    }
    mystroke.Add(myPoints);
    XMLStrokes.Add(mystroke);
   }

It compiled fine, but then the strangest thing happened. The code would never get called. If I put it in a method and had a click event call the method, the method would not get hit. I put it in a new static class but still it did not get hit. Then I put the code right in the click event and then the click event stopped getting hit.

I thought for sure it was me being stupid with C#. But finally I had a new idea and I googled “LINQ to XML” and Silverlight and the first hit was a post by Fabrice Marguerie saying that LINQ to XML was not supported in Silverlight, with a link to a post by Aaron Dunnington on the XML team.

Not supported, eh? Well, it’s just not quite yet. it’ll be there. (I knew that from reading Aaron’s post, but totally neglected to point that out. Thanks for Scott Guthrie’s reminder in the comments.) Either way, it sure had a funny way of showing it. Sheesh!

I suppose I could put it into a WPF project and test it (I still don’t know if my logic is correct) but I’ve got better things to do at the moment.

Note: I did think of using a webservice for this, but a StrokeCollecton is not serializable, so it would be redundant to find another way to get it across the pipe just to see if I can pull it off in LINQ to XML while still in the context of a Silverlight app. I’ll just be patient.

Updated Silverlight Annotation Samples for 1.0 Release Candidate

I have updated two of my demo Silverlight apps which broke with the RC version of Silverlight that came out last week.

The little embedded drawing surface at the top of this blog has been fixed along with my more full blown annoation application.

All of the changes I made were listed in the “What Changed Between Beta and RC” topic in the Help file that comes with the SDK. The one that had me confused for a while was switching the “\” to “/” in the xaml where I had images in subfolders etc. It confused me because I somehow skipped over it in the list and the error that it threw the error “2210: AG_E_INVALID_ARGUMENT” which didn’t tell me too much.

You can read more about updating your Silverlight apps to work with the Release Candidate version here.
 

Silverlight Annotation in Safari for Windows

I don’t have a Mac, but while I was working on my Silverlight annotation tests, I had a few friends try them out on Safari on their Macs and they reported that it just didn’t work at all.

Now that we have a Windows version, I finally did a few tests myself.

At first, I was being prompted to install (the already installed) Silverlight.

This post by Jeremy Boyd helped me elimitate that problem. When the page first appeared the spot where my embedded Silverlight goo should be was all white. But I saw in this post by Tim Heuer (Tim suggests another solution for the seemingly uninstalled Silverlight, but that one didn’t work for me), that resizing the browser helped and indeed, it did. I was surprised to actually see my solution appear!

Then I started clicking on the images on the right. Nothing happened. But when I inadvertently resized the browser again, the images did appear.

I was actually able to do a little drawing on the screen (not the one above!) but it wasn’t much fun. Draw a stroke. Resize. See the stroke I just drew. Etc.

So it’s close, but not there yet. I don’t think there’s much I can do but wait for some change to this  beta version of Silverlight or some change to this beta version of Safari for Windows.