Thursday, September 25, 2008

I've Moved

Nothing against blogspot, but I've moved on.

Please check out my new site at I'll continue blogging about the same kinds of things I have been, just doing it within a new structure. I'll also be focusing a little more on RIA topics in general, not just Silverlight.

Those of you that subscribe to my feed should still get my updates but I encourage you to switch over to the new feed.

The new site is still a work in process but it should allow me to do a lot more than I've been able to within this environment. Thank you all for supporting me and I hope you will move along with me.

Monday, July 14, 2008

What I did on My Summer Vacation

So it's not so much did as still doing but here's just a little update on what I'm up to at the moment, it's quite a bit.


moving Many of you already know this but I am in the process of selling my house and moving out of Ohio.  It is with mixed feelings that I do this as I have really come to love the emerging sense of community in the Cleveland development arena and the help we've had in building it from the twitter tribe.  On the other hand I'm taking this opportunity to move to anywhere I want, anywhere at all. 

I've been searching for a specific type of company for the last several months and finally decided on one that I feel is a perfect for for me at the moment.  I'm not going to say who yet, I don't want to jinx it, but it's a great web firm out of Seattle and they are flying me out for an in-person interview in a week or two.  I've already had a couple phone conversations with them as well as did a "home work" coding assignment.  So far it would seem like they like what they see, I know I do.  This is a great chance to be a full time Silverlight developer, and I'm more than a little excited at the prospect.

As part of this process we're also selling our house.  If you've ever sold a house before you know how much work it is getting and keeping everything perfect.  It's not a great market to be selling a house but we're optimistic that we'll get an offer soon.  We hope so anyway.


campbell_cover150 The writing on Silverlight 2 in Action is still ongoing.  I now see how much effort goes into the writing of a book.  There is probably a hour of research and coding for every five minutes of actual writing.  We are waiting until the release candidate comes out to finalize everything but I still have a ton of work to do even before that.  It's all worth it though.

It is known among people that teach or present that committing to do one of those actions is a good way to learn a technology.  I would argue that writing a book is even better.  You have to ensure that you know the material good enough to stand up to the criticism of peers, and trust me, we can criticize quite well.


microsoft_silverlight_c Remember a minute ago when I said that presenting is a good way to learn a new technology?  Well, watching a presentation can be a good introduction as well, so come watch me talk.  I'll be the speaker at the August 12th meeting of the Cleveland .NET SIG.

Here's my abstract for the talk: Now that most people know that Silverlight can do fancy animations and play videos, lets look at how to make it do some real work.  In this presentation we will demonstrate connecting to web services, using returned data, binding data to controls and discuss some limitations Silverlight currently has.  And of course, since it's Silverlight, we'll try to make it look good in the process.


stacked_small_slbc_08 Hopefully you have all heard about the Silverlight Control Builder Contest by now but if not, head over there and read up on it now.  Basically, you build a templateable custom control, submit it to the contest, and win prizes.  Well, you can try to win prizes anyway, a whole bunch of other people will be entering as well.  The prizes are getting better and better and include books, software, gift certificates and more.  Wait a second, the more people that enter the less my chances of winning.... Never mind, don't enter :).

How to Learn Silverlight

image001_low With a lot of people wanting to learn Silverlight today and not really knowing where to start, I thought I'd write something to help people get started.  This is not meant to be better than Jesse Liberty's version of the same thing, just a different take on it.

  1. Go read my post titled What Is Silverlight.  This should give you enough of an overview of Silverlight to make sure that it is what you think it is.
  2. Install the tools at  This will give you everything you need to begin building Silverlight applications.  Don't forget Expression Blend, even if you are not a designer some things are just a lot easier using Blend.
  3. Watch a couple "How Do I" videos on getting started and any other topics that interest you.
  4. Work through a QuickStart.  These expose you to programming numerous areas of Silverlight.  Feel free to just read the sections you need, when you need them.
  5. Build something.  It doesn't have to be original, fancy or even work well.  Don't even show it to anyone if it doesn't turn out looking quite right, but build something.  Pick an idea like a weather widget or an ad rotator or get adventurous and build a simple little space invaders style game, anything just BUILD something.  Using it is the best way to actually learn any technology.
  6. Don't be shy.  Use the forums at whenever you run into a stumbling block.  I would encourage you to attempt a search of the forums first though since if you are having trouble with something, someone else probably has as well.
  7. Read a lot.  Everybody and their brother is blogging about Silverlight.  A simple Google search will turn up information on nearly anything you can think of.  A good place to start is to subscribe to the Community blog aggregation feed from  Also be sure to catch Silverlight Cream as Dave Campbell does a great job of aggregating all the Silverlight posts he can find into a nice, searchable site.
  8. Push yourself.  Commit to doing something that you don't know how to do yet but know is possible.  There are a lot of options in this arena:
    1. Build a prototype for a work project
    2. Give a Silverlight presentation to a local user group
    3. Write an in-depth technical blog post
    4. Enter a contest.  At the moment I know of the Control Builder contest, an article writing contest and INETA is supposed to be starting the Silverlight Challenge soon as well.

I know that everyone has their own individual style and pace of learning but hopefully this will point you towards the path of [Silver]enlightenment.

Friday, July 11, 2008

What Is Silverlight?

image002_low I've noticed lately that a lot of people seem to have misconceived notions of what exactly Silverlight is and does.  I will attempt to explain what Silverlight is and how it fits into the web spectrum in language that even your boss will understand.  What I won't do is try to tell you when you should or shouldn't use Silverlight or how to build specific applications using it, those are topics for other posts.

If you have built a little prototype app in Silverlight, and certainly if you have done more than that, this post is not for you.  This post is directed at those that have heard of Silverlight, think they might know what it is and would like to know if it fits their needs.  It is also a good post to read to get a good general understanding of the way web development works.


IIS, Apache, etc.  These are web servers.  They accept the request from a client browser and return the appropriate response.

PHP, ASP.NET, PERL, JSP, Cold Fusion.  These are all server technologies.  They execute on the web server itself in response to requests from a client, typically a web browser.  They typically output HTML. 

MySQL, SQL Server.  These are the databases, they also are located on a server.  Typically the web server technology will have a method to connect to the database.

HTML, JavaScript, AJAX, Silverlight, Flash.  These are all client technologies.  They execute from the web browser itself.

SOAP, WCF, REST.  These are all types of web services.  Typically these are created on a web server to expose data and logic to outside applications.

XML, JSON.  These are data formats.

Where Silverlight Fits

As stated in the previous section, Silverlight executes in the client browser.  What does this mean? It means:

  • Silverlight applications need to be hosted on a web server
  • Silverlight does NOT need to be hosted in IIS, any web server will do
  • Silverlight does NOT need ASP.NET on the server
  • Silverlight applications cannot access databases without an intermediary (like a web service)
  • Silverlight applications cannot access server-side classes or variables without an intermediary (like a web service)

Silverlight also can run on any major browser (Internet Explorer, Firefox, Safari) on any major OS (XP SP2, Vista, OSX, Linux).

Programming Silverlight

Programming Silverlight applications can take many forms.  This is due to a number of items. 

First, the plugin contains a limited version of the .NET framework.  What this means is that you can write real, compiled and managed code that executes in the context of the client browser itself.  What it doesn't mean is that you have access to the full framework.  There are a lot of areas of the framework that are missing completely, like System.IO and others that are present but are missing features, like no ArrayList for instance.  It also doesn't mean that you can build reference an already existing class library from your Silverlight application.  You can still use class libraries but they must be compiled against the Silverlight version of the framework.

Silverlight also introduces what is known as the DLR, or Dynamic Language Runtime.  This allows you to write code in non-traditional .NET languages such as IronPython or IronRuby.

Silverlight Versions

The most current version of Silverlight that is in full release is Silverlight 1.  Silverlight 1 is very good at displaying Media (audio and video) but it's lack of controls and reliance on JavaScript as the only programming language, introduce numerous limitations.  For a first generation plugin though, it isn't bad at all.

Silverlight 2 is currently in it's second public beta and is due to be in full release this fall.  It also contains a full commercial "go live" license, which means that you are permitted to use it to build real production, money-making applications with the Beta version.  Silverlight 2 introduces the .NET framework to Silverlight along with the DLR and a rich control set.  The majority of applications being built today are using Silverlight 2.  With all of the advances in Silverlight 2 combined with the go-live license and the short timeframe until the full release, building anything in Silverlight 1 today seems wasteful to me.

Silverlight and WPF

Silverlight is a limited subset of WPF, both of which use XAML as their markup language.  This allows for several things, such as the shortening of the learning curve when moving from one to the other.  It also means that typically, if you develop an application using Silverlight, it can normally be ported to run as a WPF desktop application with minimal effort.  Moving the other direction however, from WPF to Silverlight, can be more troublesome due to the greatly increased .NET framework and control set in WPF.

Silverlight and ASP.NET

ASP.NET and Silverlight overlap in some of their capabilities, but Silverlight is not a straight replacement for ASP.NET.  Since code written for ASP.NET executes on the server it has access to the entire .NET framework, can use connection strings to access databases and can use any number of third-party or internally generated class libraries.

Even on the client, there are times when you may want to stick with ASP.NET over Silverlight.  If you have parts of your application that are nothing more than data-entry forms, the standard ASP.NET controls are very well suited to that kind of work.

Silverlight and Flash

Everyone has heard talk of Silverlight being Microsoft's "Flash Killer".  Personally, I do not think that is the intention or even goal of Microsoft when it comes to Silverlight.  I believe that they (MS) saw a way to add more interactive capabilities to web applications in a way that is friendly to .NET programmers.  I see the emerging RIA space as being plenty large enough for Flash/Flex, Silverlight and even a few others.  Yes, they both do many of the same things and yes, Flash may do some of them better currently.  But give Silverlight some time to mature and you will see much more feature parity between the two.

Silverlight and AJAX

Built into the Silverlight plugin and SDK are numerous features that perform the same functionality as that provided by AJAX, but with the need to write complex JavaScript.  Silverlight has very rich support for reading data of numerous types from web services to RSS feeds to static XML files.  It also has methods to facilitate the deserialization of XML or JSON into strongly types objects for simple manipulation and binding.


Hopefully this post has helped answer some of your questions as to what Silverlight is, what it isn't and where it fits into the web space.  If you have more questions or want to correct me on something I may have mistyped, please leave comments here or use the forums at

Thursday, June 19, 2008

Silverlight 2 In Action - I'm Writing a Chapter!

So as you all know I've been posting a lot about Silverlight lately.  Well not recently as I've been a tad busy.  I've been quite lucky to get to work on Silverlight at my day job which has greatly increased my knowledge of it in a short time.  I've also been pretty active in sharing the knowledge I've gained with other developers through this blog, live presentations and the forums at

Apparently all that effort was noticed because a couple weeks ago Chad Campbell asked if I would be interested in writing a chapter for Silverlight 2 in Action.  Well, of course I jumped at the opportunity.  Oh, and he needed the chapter written in less than two weeks.

Having never written a book before I had no idea how much work it was.  I knew two weeks was aggressive but wow.  I've spent pretty much every waking moment since the evening of 6/9/08 either at my regular job or sitting at my laptop writing.  Who knew that writing could be so tiring?

The chapter I'm writing is Chapter 6 - Networking and Communication.  This means that I have to write a ton of code samples, make sure they all work and then boil them down to just the parts that are needed.  There is a lot to this chapter too.  I've been writing about things like duplex WCF services, transferring JSON data from REST services and, of course, clientaccesspolicy.xml, everyone's favorite file :).

With the chapter almost wrapped up I should be able to start writing informative blog posts again, I've got a couple ideas for posts that should be pretty helpful.

One last note, Manning Publications has a program called MEAP where you can get the rough draft of the book in PDF form before it is published.  That means you can actually go buy a real Silverlight 2 book today!  Don't worry, you will also get the PDF (or print version if you prefer) of the final book when it is published.  You can get to the MEAP version of the book here.

Well, I've got to get this chapter done before the editor hunts me down :).

Friday, June 6, 2008

Silverlight Installation on Intranet Hosted Applications

Silverlight_Logo_thumb A few months back Tim Heuer wrote up a great post on customizing the Silverlight installation experience.  I highly recommend reading his post and doing something similar in your own applications, it can really make the user experience better. 

What I'm going to discuss builds on what Tim already wrote so go read his post first if you haven't done so already.  Don't worry, I'll wait.

The Problem

Welcome back.  I was recently presented with a situation that isn't exactly common, but is not so unusual that no one else will experience it, in fact, some of you may be working on this right now.  The situation I was presented with is we are building an ASP.NET application that contains a Silverlight application and is installed at client locations via a installation CD.  At some of the installed locations, end user PCs that will use the application don't have access to the outside Internet.  So how do we install the Silverlight Plug-in if the browser can't access the Microsoft download site? 

Hopefully the corporate IT department will have already pushed the Silverlight plug-in to all PCs in the enterprise.  There was a nice white paper put together last year explaining different approaches for doing just that.  Unfortunately, we all know that many IT departments are slow (or reluctant) to install new software, which is why we have this issue.

Since the application is installed in numerous locations from a single installation CD, we don't know the environment it will be installed in, so any solution had to handle cases both with and without Internet access.  After attempting numerous approaches I came up with something that is so simple and elegant I felt a little stupid for not coming up with the answer hours earlier.

The Solution

The first step is to ad the latest version of the Silverlight Plug-in installation .exe in your web app.  You could just point your installation link to that exe and call it done but that's an easy way out.  It would be better to have the user install directly from the Microsoft site to ensure it is the latest version that gets installed.  So what we needed was a way to determine if the users browser can access the Microsoft download site and then install the runtime from there if available or from the intranet application's web server if MS is not reachable.  This is actually quite easy.

There are two parts to the solution:

Object Tag

As Tim stated in his post, you can put quite a bit inside the object tag that hosts your Silverlight app that doesn't get rendered if the browser has Silverlight installed.  In this case I kept it very simple (basically the default object tag).  All I changed were the location the image gets loaded from and the destination of the link.  The image is now coming from the current application and the link points to a JavaScript function, which you will see next.ObjectTag


The JavaScript is also easy.  You will notice that the first two lines are not inside a function declaration, that means that they are executed as soon as the browser parses them during page load.  What those two lines are doing is attempting to load an image from the download site.  Those of you with much web experience will notice that this is the technique we use to pre-load images for rollovers and similar effects.

The function then, which is called only if the user doesn't have Silverlight installed and clicks the "Install Silverlight" link, checks to see if the image was successfully downloaded or not.  If the image is present it means that the user does indeed have access to the Microsoft download site and directs the browser to the correct location and if not, directs them to the exe in the Intranet application.



As you can see this is a very simple way to make sure that users of Intranet hosted applications can still get the Silverlight plug-in, even if they don't have Internet access.  If anyone else has similar tips please pass them along.

kick it on

Thursday, June 5, 2008

How I got started in software development

So a twitter friend, Michael Eaton, started something that sounds like fun, and before I could get my answers posted a few others have as well.  So here it is, the Reader's Digest version of how I became a programmer.  Please post your own answers as well, this is a fun way to get to know each other.

How old were you when you started programming?

I think I was about 13 when my dad bought a TRS-80 Model III.  That awesome machine had dual 5 1/4 floppy drives, 48k (yup, 48k) of RAM and no hard drive.  I learned BASIC so I could program simple little animations and things.  The only problem was that if you forgot to boot from a floppy, the drives weren't initialized and you would have no way to save the tiny program you just spent hours coding.  I don't miss those days.

How did you get started in programming?

I don't know, maybe I was always a big dork? :).  I learned it as a teenager since it was the newest thing around, I think that even at that age and time (would have been around 1984) I knew that computers were going to be big. 

In college though I spent the first couple years as a History major, not taking a single programming class.  I did have a laptop though, which was unusual in 1995, and do simple desktop publishing and stuff for friends at the coffee shop.  Finally after the 100th person asked me if I were a computer science major I realized that maybe I should be.

What was your first language?

That would be BASIC, not QuickBasic or Visual Basic but plain old BASIC.

What was the first real program you wrote?

The earliest real program I can remember was a phone number database built using VBA on top of Access.  It was for a small college where I was doing tech support and started as a way for the switchboard operator to have quicker access to find the correct numbers.  Eventually I even built a web interface for it and added it to the school intranet (which also didn't exist before that).

What languages have you used since you started programming?

Lets see (I'll be generous in my definition of language to make the list longer :) )

  • Basic
  • VB
  • C++
  • Java
  • HTML
  • JavaScript
  • VBScript
  • BHTML (Who out there know where this comes from?)
  • XML
  • C#
  • XAML
  • T-SQL
  • PL/SQL

I know I'm forgetting some but that should be most of it.  I know that some of those aren't 'real' languages, but they seemed to fit for this list.

What was your first professional programming gig?

At the college I mentioned above I ended up doing quite a bit of programming of little utilities and the school website but I was never officially a programmer.  It wasn't until 1999 and I was hired by a web design/consulting company called Mozes Cleveland & Company.  That company grew like mad, had a ton of clients and promptly went out of business in the summer of 2001, like so many other companies of it's type.  I still think fondly of those days, they were fun.

If you knew then what you know now, would you have started programming?

YES!  Six months ago I don't know if I would have had the same answer but two things have really rekindled my programming spirit: Community & Silverlight.

If there is one thing you learned along the way that you would tell new developers, what would it be?

Never be satisfied.  Always work actively to improve yourself and your code.  Never consider a project complete until your manager tells you to stop, it can always be made better.  Never stop learning, complacency is what lets you get passed over and allows boredom to set in.

What's the most fun you've ever had ... programming?

I actually already wrote about this one in a old post.  Basically being sent to the Netherlands, working on a truly international team and writing some cool code, nothing else has come close... yet.

Wednesday, June 4, 2008

Cleveland Day of .NET 2008 is History!

ClevelandDODNOn Saturday, May 17th, we held the first Cleveland Day of .NET and it was, from all accounts, a success.  It sounds like everyone had a good time, met a lot of new people and learned about technologies they would not have been exposed to in their normal jobs.  This is exactly what we were trying to accomplish.

How it got started

All of this came about because a few community members were talking on twitter about Cleveland needing a Code Camp or something similar and deciding to do something about it.  Myself, Sarah Dutkiewicz and Michael Letterlie, none of whom new each other in person, decided to create this.  A few days later was an ArcReady where Joe Fiorini and Corey Haines also joined in.  By the time we had our first meeting Sam Nasr was also an organizer and by the second meeting, Mike Slade was with us as well.  From the very first meeting, where most of us actually met each other, until the Cleveland Day of .NET took place was just under three months.  This group of people pulled off a great conference without any of us having done anything like this before.  If you see any of these organizers out in the community, thank them, they all deserve a big congratulations.

A Good Day

In the weeks since Cleveland Day of .NET I have heard directly and indirectly from many people that all say it was a good event.  Sponsors, speakers, attendees and organizers alike all feel that it was a good day.  Of course there are things that can be improved upon for next year, that is true of any event, but the day did accomplish what we set out to do: Provide a locally hosted developer focused event for fellow developers.


In addition to the people that worked so had to pull this together, plenty of other people are deserving of our thanks.


When we were first organizing this, we dreamed big but were skeptical of getting very many speakers.  We were wrong.  After deciding on time and tracks there was room for 24 sessions and 4 vendor sessions.  When we placed the call for speakers, hoping to get 24 good responses, we had almost 50 talks proposed, it ended up being quite a task to trim that down to the 24 that we had room for.

Some drove long distances, some spoke at very little notice, some were speaking in front of a group for the first time.  All of them did it completely for free, no gas money, no free hotel.  Ok, so some of them may have just come for the opportunity to drink with fellow geeks :).

Please thank all of these speakers when you see them around:


Without the support of the local development community this event would have been pointless.  Both of the relevant user groups, the Cleveland C#/VB. NET SIG and the Cleveland .NET SIG helped to spread the word and offered any support we needed.  The community members that attended put up with the narrow hallways, lack of lunch and no good meeting place yet offered nothing but praise.


There was also a lot of support for Day of .NET from local companies as well.  Without the generous sponsors all the effort put in would never have been enough since it always ends up coming down to money.  We had a great bunch of sponsors that all were very supportive and adaptable, even when we weren't sure what we were doing.  If you would like to thank them yourself (and please do) here they are:

We also had a number of companies donate books, software and swag to give away:


It's all over now, we're all finally caught up on rest and back to life as normal.  But let's not let the momentum die, we've started something here, lets keep it going.  And I'll see you in the community.

Wednesday, May 7, 2008

Review: ArcReady 4/30

So this review is a little late (and a little short) but don't worry, I took notes :).

This was Brian Prince's first appearance at an ArcReady event as the Microsoft Architect Evangelist and well, he did a great job.  Personally I don't know how long he can call himself "the new Josh Holmes" because personally, I dug this presentation more than most of the ones Josh gave.  I could be biased though since this topic is one that directly relates to my job more than most have.


The title of the presentation was "Architecting for Scalable and Usable Web Applications" and was broken into two sessions, one on scalability and the other on usability.  The first session was definitely the better of the two in that it had more meat protein.  I would say that this is partially due to the fact that there are more concrete ways to tackle scalability than there are on usability design.  Usability is much more abstract, a "you know it when you see it" kind of thing.

Both sessions had many techniques to try and tools to use when tackling their respective issues.  Personally I learned a lot from both sessions, I will use many of the items in the future, no question.


Brian did a great job.  If you've never seen him speak, you are missing out.  He comes across as clearly knowledgeable yet humble, is free to admit when he doesn't know something, and is guaranteed to throw in some obscure Sci-Fi reference that most of us understand but wouldn't have been able to pull out on the spot.  He also has a way of speaking that keeps you engaged and learning, which for some tech talks is no minor feat.  In our area Brian is giving a talk on soft skills at Cleveland Day of .NET on May 17th, come see him, you'll be glad you did.

Day of .NET Is Almost Here!

IClevelandDODNt's almost here! 

The first Cleveland Day of .NET is in a week and a half (assuming you read this the day I post it).  Everything is moving very quickly now.  Sponsors have stepped up to donate the money to pay for the venue, t-shirts and all the other things.  So many great speakers came forward from the entire Heartland District that we had to turn half of them away.  With only word-of-mouth advertising, there are nearly 200 attendees registered.  All of this has been pulled together by a small group of dedicated volunteers.

Generous Sponsors

In the last few weeks we have had a lot of great sponsors come forward.  Without them this event wouldn't have a chance of succeeding.  Please visit their tables, attend their presentations and listen to their pitches.  Visit our site for the most up to date list of these sponsors.

Great Speakers

We had nearly 50 proposals for talks but only have the space for 24 sessions.  This made it very difficult to choose which talks to accept and which had to be cut.  I can honestly say that not even one talk was cut because we didn't think the presentation or the speaker were appropriate or skilled enough.  The list we did come up up with covers both new and upcoming technologies like LINQ and Silverlight but also what people are using today like ASP.NET and SharePoint, we even have a few methodology style talks in there.  The only thing I see as wrong with the schedule is that I can only physically attend one talk at a time :).

Almost at capacity!

As of this morning there were 192 people that had registered and indicated that they would attend.  Another 16 had registered but have since said they cannot make it.  What this means is that there are only 58 more slots for registrations before we have to cut it off and turn people away.  If you haven't registered yet, or keep forgetting to tell your co-workers about it, time is running out.

Hard Work

A lot of people have been working very hard to bring this thing together but it is finally almost here.  We all truly hope for this event to be a great success and for people to come away with a wealth of new knowledge and a better appreciation for the local development community.  That sense of community, or rather a lack of it, is what caused us all to get together and start planning this event.  Please come and learn about some technology, tool or technique to help you in your job, and meet the fellow developers that can help you in your career.  Oh, and lets all have a good time doing it!

Tuesday, April 29, 2008

Silverlight 2 TextBlock Responds to Mouse Events when Truncated

ANOTHER UPDATE: I received confirmation from Joe Stegman that yes, this is indeed a bug in Silverlight 2 Beta 1 but has been fixed in the forthcoming Beta 2.

UPDATE: This behavior can be fixed by adding a clip to the bounding control which in this case is a Rectangle.

I finally had time to sit down and really debug the Silverlight animation problem I described in a previous post.  I recommend heading over there and reading that post, it also displays the problem in action.

The issue is that when using the Height or MaxHeight properties to restrict a TextBlock, if the text is longer than is allowed by the defined size, even though visually the text is truncated, the area in which the text would be still responds to mouse actions.  It does not matter if the properties are set on a parent object like a StackPanel or on the TextBlock itself, the issue is the same.

Unless I'm missing something if the text is visually truncated, the non-visible area should not respond to mouse events.  If anyone knows of a reason for this or a way to fix it, please let me know.

Below is a sample app demonstrating the problem.  To see the grid working normally, move the mouse around and watch the squares change color.  What should happen is that the square that the mouse is in turns red, and when the mouse leaves the square goes back to green.  If you click an item so that the text changes, then the mouseover action changes.  Now the area about two and a half squares below the selected one will also turn the square red instead of the square that the mouse is over.  If you click the square again to set it back to normal, it acts as it should.

You can download the code for the example from here.

Friday, April 25, 2008

Silverlight 2 Beta 1 Problem with Animations on User Controls

YET ANOTHER UPDATE: I received confirmation from Joe Stegman that yes, this is indeed a bug in Silverlight 2 Beta 1 but has been fixed in the forthcoming Beta 2.

ANOTHER UPDATE: I found what is causing this, you can read about the exact cause and download sample code in a newer post.  If this is a bug in Silverlight or if there is a nuance with the TextBlock of which I'm not aware has yet to be determined.

UPDATE: This is most likely NOT a bug in Silverlight itself but probably has something to do with how I am implementing it.  I will post a detailed description of what caused it and how to avoid it as soon as the cause is identified.

Let me start by saying that this issue is one that I doubt will be seen by many people, even if it does persist into the RTM version.  Even describing this issue without using lots of pictures (or better yet a (mal)functioning sample) is not an easy task, so let me see how well I do.


As I said in a previous post, I started a CodePlex project to build a session scheduler in Silverlight.  Within this application there is a User Control defined that represents a single session which includes the information about the session, the graphical representation of that data and an animation.  When the app reads in data from an XML file it creates a number of instances of this user control in specific locations in a Grid.

Matt Casto joined the project this week and the first thing he did was to create a more elegant way to catch the MouseEnter, MouseLeave and MouseLeftButtonDown events by adding them to the user control declaration tag instead of to the transparent rectangle that I had been using previously.  This made for a nicer experience as the area that handled the events would expand with the control during the animation.


From the picture above you can see what the animation looks like in it's final (open) state.  When in this state, there is a path that overlaps a section of another instance of the user control below the currently active one.  With the new behavior if you move the mouse into this path covered area of the active user control, nothing happens since the mouse is still within the defined area of the user control.  So far this is all good.

The problem arises when you have one instance of the user control in an open state, then move the mouse into the area of another instance of the user control that would be covered by the expanded path.  The area I'm talking about is the top/left part of the small version of the control. 

The result is that if ANY animation is in it's open state, the instance of the User Control that would be covering that section is activated, not the instance of the control that the mouse is actually over.


I've reported this issue to Joe Stegman and may also post it to the forums at  If I hear from Joe or someone else that it's actually a problem with our code and not a real Silverlight 2 bug, I'll gladly update this post with that information.

This one is most likely a rare issue that will not effect the vast majority of Silverlight applications, but it does appear to be a real issue.  For our application we decided that we can live with the odd behavior but for a business application that would be a hard case to make.

Thursday, April 24, 2008

Review: Cleveland C#/VB.NET SIG 4/22

sig_logo Once again this review is coming a couple days late, I'll try to get a little more punctual but since I've been working on that for decades, don't count on it any time soon.

First, the venue/crowd.  It was held at the Berbee office in Brecksville as usual, although most likely for the last time.  Sam Nasr, who coordinates this SIG, is leaving Berbee so will be looking for a new location (if you want to host it contact him through the site above).  The room was fine although it looked like we were about at max capacity for the room with around 30-35 people.  Pizza was a little late but no one seemed to mind, at least not that I noticed.  It also got a little warm but with that many people in a small room that is to be expected.  And they were all staring at me, so maybe I was the only one warm.

This month at the Cleveland C#/VB.NET SIG the presentation was Silverlight 2 in ASP.NET presented by well, me.  This was my first time giving a presentation like this in a long time so I was a tad nervous that day.  Ok, more than a tad, I was VERY nervous. I felt like my presentation was weak and that I wasn't ready to give it.  I may be my harshest critic (at least I hope so) and in thinking about it later I definitely have some things to work on, but overall I think people learned a lot from the presentation.  I actually surprised myself with the number of questions that I actually knew the answer to, and there were a lot of questions.  Lots of questions.

What did I do wrong?

  • I was unorganized
    • I had notes to keep me on track but didn't follow them
  • I let questions derail me
    • I would go too deep into an explanation and had a hard time getting back on track
  • I didn't prove my thesis
    • I don't know that anyone noticed this until I pointed it out over beer.

The good part about everything that went wrong is that I know how to fix it. Practicing in front of real people will help quell the nerves and let me pay attention to my notes.  Practice will also help me handle off-the-wall questions better.  A small change in my slides and a more focused demo will prove the thesis better.

Overall the feedback I've been getting from people is that is was a very good presentation.  Personally I know I have a lot of room to improve, but you have to start somewhere.

P.S. Go register for Cleveland Day of .NET, I'll be posting some exciting updates about that event soon.

Sunday, April 13, 2008

Silverlight Session Scheduler at CodePlex

Guitar2 As many of you know, I am one of a group of local developers that is organizing Cleveland Day of .NET, a free one-day developer conference on May 17th.  As part of this we wanted a cool way to allow for registrants to select the sessions they want to see from the conference website.  Naturally when the words "cool" and "web" are in the same sentence, Silverlight comes to mind.  So I built one.


What I came up with is a simple way to view and select sessions for a one-day conference.  I hope to expand it to multi-day capable eventually.  It marks your selections and also shows them in a list to the side.  Eventually I would like to make it easy to print and submit your selections back to the server.  None of that is in it yet but still, I think it is kinda cool.


The above image is just a screenshot.  The actual one does some neat stuff though, like an animation on mouseover to display more information.


And when you select an item, two things happen: 1) an overlay with "Selected" is placed on the box and; 2) The title is added to the proper place in your schedule list.


Open Source

In the true nature of Cleveland Day of .NET, I've posted the entire source for this app at CodePlex under the name Conference Session Scheduler.  Feel free to join and help me add more features or fix what's there.  You can also just download the code and use it as is if you want to, the choice is yours.  This is a good project to learn about User Controls, global variables (i.e. singleton object) and loading objects based on XML data. 

I'll pass along one tiny piece of code in this post:  How to set a Fill color from a string color value in code.

this.myRectangle.Fill.SetValue(SolidColorBrush.ColorProperty, "#FF386E1B");

I know the above looks simple but believe me, finding it was anything but easy.


If you have the Silverlight 2 Beta 1 plugin installed you should be able to see the scheduler in action.  I won't promise to keep this up to date as we update the code but at least it is as of 4/13.

Ok, so this is already out of date but I promise I'll replace the one in this blog with the real Cleveland Day of .NET schedule when it is posted next weekend.

  kick it on

Friday, April 11, 2008

Silverlight 2: Custom Controls vs. User Controls

Silverlight_Logo_thumb In my first post on Silverlight 2 I concentrated on how to use User Controls in a Silverlight application.  Since I started writing this post, Scott Guthrie and numerous other developers have also written posts on User Controls.  One thing neither of them cover though is when to use them.  With most technologies there are numerous way to accomplish many tasks, Silverlight is certainly no exception and one example of this comes in the form of User Control vs. Custom Control. 

This post is different than my other Silverlight posts as I'm not showing any code and don't have a cool example to talk about.  This post is meant to be more of a discussion on the differences between User Controls and Custom Controls as well as some guidance on when to use one over the other.  Much of what I'm writing here is just opinion though so if you have a different though or if you find something that I have completely wrong please comment.


First off let me say that I haven't used Custom Controls much yet so that may sway me a little but I'll try and be open minded.  I'll also say though that it does appear that creating a custom control in Silverlight 2 is significantly easier than creating a custom control in ASP.NET.  Much Easier.

Custom Controls and User Controls both accomplish many of the same goals although in different ways.  It seems to me that this question is nearly the same in Silverlight as it is in ASP.NET, and I suspect with much the same answer.

User Controls

The Silverlight documentation gives two reasons for creating a User Control:

  1. To separate functionality into smaller, manageable pieces of logic that can be created independently from an application and other controls.
  2. To group related controls that can be used more than once in an application.

In practice, user controls are used extensively for both of the reason listed.  One of the great things about them in the ASP.NET world, and now in also in the Silverlight domain, is the ease with which they can be created and reused.

Custom Controls

Custom controls can be loosely defined as creating a control that is based on (i.e. derived from) an existing control and extends its functionality in some way.  Some reason they may be built are to add a capability not included in the base controls, make a company specific version of a control or to group several controls together to perform a specific function.  This last version, grouping several controls to perform a specific function, where the big overlap with User Controls occurs.

One other advantage of custom controls is that since they are compiled into a .dll, they can be reused in multiple Silverlight applications where a User Control is limited to the application in which it is created.


While I was working on a new Silverlight 2 project a few weeks ago I had this scenario:

I want multiple blocks to have the same base behavior, such as being resizable, moveable and a similar look/feel.  However, I wanted the contents of these blocks to be different.

With the custom control vs. user control question I went to what is becoming an incredible resource when you are looking for advice but not big text references: twitter.  I happened to know that at least three recognized Silverlight experts (TheADOGuy, LBugnion and WynApse) were on twitter at that moment and that they were following me so I sent a question which resulted in the following conversation (minor editing for clarity):

johnnystock SL2 gurus: If I want to make a custom container object to handle things like drag/drop, look/feel etc. should it be user or custom control?

LBugnion @johnnystock Does your container have an intrisic look&feel, or is it going to change its look often?

TheADOGuy @johnnystock I suggest you derive a custom control from "Panel" but not knowing all your req's that might not be perfect

johnnystock @LBugnion Look/feel will be same for all instances

johnnystock @TheADOGuy Is there a SL2 panel? I was thinking of deriving from Canvas

LBugnion @johnnystock I use UserControls when the look&feel of my control is more or less similar (changes can be handled by styles)

LBugnion @johnnystock and CustomControl for truly lookless controls, where L&F changes a lot depending where they're used

LBugnion @johnnystock That said, the sugestion to make a custom Panel sounds pretty good!

johnnystock @LBugnion That's what I like to hear as personally I find User Controls easier than custom controls :)

LBugnion @johnnystock If you check MIX sessions, Robbie Ingebretsen shows how to create a custom WrapPanel. A good start for what you do

LBugnion @johnnystock Custom Controls are harder to understand, but flexibler to use.

johnnystock @LBugnion cool, thanks for the tips and I'll check tat session out

LBugnion @johnnystock but I do a lot with UserControls too.

TheADOGuy @johnnystock Yes, its the abstract class under all the containers...look at the class def of Canvas in the .chm...

LBugnion @johnnystock The session is Nerd + Art, around 00:46:00

From the above discussion it doesn't really look like there is always a clear cut line on when one type of control is better than the other.  Well, I think there are cases where custom controls are clearly correct, and cases where user controls are clearly right, but there is also a quite large overlapping gray area.  The gray area is seems mostly populated by container controls as well.  It seems like developer preference definitely plays a major part in the decision when in the gray area. 

For what I'm trying to accomplish I will probably be using both.  The functionality will be broken into user controls but the container that houses that user control will be a custom control.  In fact, the only reason I'll be using user controls at all is to reduce clutter and make multi-person development easier as the functionality will not likely be reused.  In my opinion those are valid reasons to use a user control, even though they are not the stated purpose of them.


I am by no means an expert on custom controls but I do have a lot of experience with User Controls and for me I don't know that there is a simple line to choose between here.  Personally I use something like this to help make the decision:

  1. Will this control be used in multiple applications? - probably Custom Control
  2. Is there a base class that has the majority of the functionality needed? - probably Custom Control
  3. Is rapid development a factor? - probably User Control
  4. Am I building it or just specing it out? - probably User Control if I'm building
  5. What am I more comfortable coding myself? - User Controls

Does anyone else have any criteria they use?  I'd love to hear feedback on your preferences as well.


UPDATE: Laurent Bugnion has posted a nice article explaining how to bundle a Silverlight User Control in an assembly to use across applications.

Digg!   kick it on

Thursday, April 10, 2008

Review: Cleveland .NET SIG 4/8

So I'm a couple days late in writing this, but it's ok, I took notes :).  Overall this was a good SIG, would have been better if I actually cared about BizTalk though. 

Why Review

First off let me explain for a second why I feel like I need to write a review of these events.  Personally I think I get two things out of writing these.  First is the chance to tell others that didn't attend about all the cool stuff they missed out on.  I think this is a great way to advertise future events by talking up past ones.  The second thing gained is that by analyzing the past, we can learn things about the future (can you tell I was a history major at one time?).  In this case, by looking at what went right and what didn't, we can come up with ways to improve in the meeting next month.  Or someone from Kalamazoo or Austin or Sacramento might get ideas for their own use group.


The venue was the Cleveland Microsoft office in Independence (notice, not actually in Cleveland) and we had both Developer Evangelist Jeff Blankenburg and Architect Evangelist Brian Prince.  This was Brian's first SIG/UG trip to Cleveland since becoming a softie.  The usual pizza and Coke was present as was the worlds worst collection of giveaways in the history of SIGs, but more on that later.  The only venue related issue was the A/C needed a kick in the pants which Brian was happy to provide.


This month Richard Broida gave a talk titled "The Intelligent Programmers Guide to BizTalk" and if you remember last month's review he talked about tying to get the crowd more involved.  First on the crowd involvement.  I will give Richard credit, he definitely tried to mix it up and for the most part I would say it worked.  Nothing is going to change completely overnight but this was a good first start.  Some of the things he tried were being more conversational, walking among the crowd a bit and joking around.  Also he didn't jump straight into the talk, he started by introducing the MS guys and asking for announcements, which of course I had one.  I got up and spoke for a few minutes about Cleveland Day of .NET trying to get people to register, speak or sponsor.  Jeff even got up to encourage people to speak.  Overall between the numerous people talking at the beginning, Richard joking around and his walking around, it really did get people more involved.  Personally I think there is more to do in that arena but this was a very good step in the right direction. 

Another step in that direction of course is the after-party.  This month we had 14-15 people, not bad.  We had a few people that were new to the bar and had no previous connection to anyone in the group, which indicates it isn't becoming a 'click' but that others do feel welcome.  Once again I left way too late but enjoyed every minute of it.  Poor Brian had to drive all the way to Columbus after the bar, which we didn't leave until 11:00PM.


Honestly I can't really judge the quality of the presentation as I'm not a BizTalk person.  At all.  Don't want to be one either.  One thing that I think came up at the bar was bullet points.  If you've read Beyond Bullet Points, well, it says to basically not use them.  I counted 27 bullet points on one of the BizTalk slides.  Now I'm not going to say that's bad since that is the norm for presentations today.  Two things about the presentation that I didn't like: 1) it felt like too much information for one presentation 2) there wasn't a break in the middle.  Any talk that long needs a break to let people stretch and socialize in the middle.  As I said, I can't really speak to the content but judging from the questions presented, Richard does know his stuff and showed numerous features that interested people.  I did learn one cool thing: functoids.  Not exactly sure what they do but it just sounds cool, Functoids.


I said earlier that the giveaways were well, bad.  Not all of them but overall, it lacked.  There was a backpack I thought was kinda cool, a couple copies of Halo 3 and, get this, two Windows Mobile Berets.  That's right, a beret with a MS Windows Mobile logo.  Wow.  The drawings started as you would expect, first a copy of Halo 3, then the backpack.  Next was something I don't think I've seen before, the next person 'passed'.  He didn't want anything that was left (Halo or Beret).  The next couple people also passed.  Finally someone took Halo, then the first beret went.  Then a couple more passed until finally Jeff just gave the remaining beret to the first person that would take it.  That person happened to be Joe Fiorini and he wore it all night at the bar.  Jeff took a picture of Joe in the beret, he's probably going to pull it out at an opportune moment.

Overall it was another good SIG and a good after-party.  Less than two weeks until I talk at one of these, hopefully I'll be ready :).

Wednesday, April 2, 2008

Silverlight 2: Simple Animation

I'm not a designer.  Would have trouble even opening a professional design or animation tool.  But even I can make animations in Silverlight using Expression Blend.

Now, I didn't build anything big or fancy, but I think it's kinda cool.  Most of all, I did it all myself with no help from designers or even the web or help files.  I did 99% of it within the Expression Blend 2.5 environment, only had to go to Visual Studio 2008 to wire up an event handler, and that was only a one liner.

So, who is ? Click his name to find out. (If you see the download Silverlight logo or a blue bar instead you need to do that first).  Then click the name.

Did you click it?  Go ahead, do it, I'll wait.

So it's not big and fancy but it does show a neat effect you can use Silverlight for.  It's also a great example of using Silverlight for a very small piece of a page as opposed to the full RIAs that are being showcased.  I uploaded this tiny app to Silverlight Streaming to allow me to use it in this blog where I don't have access to the server or even the header of the page.  As you can see it works :).

How did I do it

I only had to touch two files for this little app, page.xaml and page.xaml.cs.  I did cheat a little bit by using a mono-spaced font to make the letter positions line up at the same location regardless of the letter width.  If your not sure what I mean take a look at these sets of letters in two different fonts:


Can you see how in the second font the letters are all the same width where in the first there is a very noticeable difference?  By using the mono-spaced font I was able avoid spacing issues.  Did I need to, no, but it made it easier.

Create the objects

Using Expression Blend 2.5 I created a Silverlight 2 Application that gave me the basic page.xaml and app.xaml files.  I then got right at creating the xaml objects.  First I replaced the default Grid root control with a Canvas instead.  I may not have needed to do this but since I was dynamically placing them and not using columns or rows, it seemed appropriate.  Next I added a rectangle to fill the background.


Again, I could have completely skipped this step and had a transparent background, but I know it was going to be on a white page so it made sense.  I then added a separate TextBlock for each letter each of which with the exact same height, width and vertical placement.  They were all exactly the same distance apart as well.


I then added a rectangle over top it all to act as the clickable area.


Build the animation

BlendInteractionWith all of the xaml objects created, I could start on the animation.  Again using nothing but Expression Blend, I first created the storyboard.  Then I selected the first letter (TextBlock) and added a keyframe at 0 seconds and another at 1 second.  I then selected the letter and drug it to it's new location.  Using split view when editing animations helps so you can fine tune values.  This made getting the letter to the exact spot I wanted quite easy.  To make the letters flow a little nice I added a couple more keyframes immediately after the first and before the last.  On to these I set the opacity of the textblock to be 65% and on the first and last keyframe I set made sure it was at 100%.  Running it a few times I was satisfied with the look so I then followed the same process for each of the other letters.

One thing that happened during this I found odd:  Even though I followed the same process for animating each TextBlock, the first half of them modified the Canvas.Left property while the others applied a RenderTransform along the X axis.  Both approaches bought about the same result but I still would have changed them to all be the same in a production application.

Wire the event

The only thing left was I needed a way to start the animation.  For this I selected the final rectangle and in the properties tab I selected the events pane.  Once there all I needed to do was double-click in the MouseLeftButtonDown text box and Visual Studio 2008 opened with the page.xaml.cs file open and the stub method already created.  Then it is as simple as calling the Begin method on the storyboard.


I did do one other thing in code actually and that was in the constructor for Page, I set a couple of properties on the storyboard to get the reversing behavior and the pause between directions.

That's all there is too it.

kick it on

Thursday, March 27, 2008

Old Code Part 4: I thought it was a Joke

Question Mark image by jhhwild on flickr About three years ago some senior executives at our company were in town for a big planning meeting.  As frequently happens during these times the entire office went out that night to show the visitors a good time.  During that get-together, the director of our office came to me with a question: Is it possible to have our web based application open a desktop app on the server and have a user over the web control that application.  I basically and politically said no, with the standard disclaimer that with enough time and money anything is possible of course.  So I thought that was the end of it.

The next afternoon I'm sitting at my desk working and in walks the director who promptly asks how I'm doing on making the web app launch and use the other app.  I was kind of stunned as I thought the topic was done but I start researching at the point.  Pretty much everything I can find either says it can't be done or you are stupid to even try.  The best answer on Google Groups was to quit working at a place with such unrealistic expectations.

So I took my findings to my boss who says "Yea, that's what I'm hearing from other people too, but you still need to make this work."  And with that I went to work on a prototype.

Over the next week I learned about using p/invoke to launch a windows app.  It was a little odd, being a web developer but within a week I was able to launch the windows app and send keystrokes to it.  Having proven that at least that much was possible my boss then assigned a couple more developers to work on this.  Luckily one of these developers had a windows background and so he took care of the lower layer.  We then had the prototype actually scraping the windows app and passing the values up through a windows service, to the web app.  At that point the web app would convert the values into screen coordinates and control types, and populate then with values.  We even had the app sending each keystroke from the client browser to the web app via AJAX and whenever the windows app updated itself we would refresh the screen to update the visible controls.  It would also spawn multiple instances of the web app, one for each user.

We had accomplished what we had thought was not possible!

When we showed this to the director, he was glad that we got it to work but to me didn't seem as impressed as he should have been :).  He did go off and tell the Japanese sponsors of the project about it though and before we knew it we were on a 8:00PM WebEx with Japan showing off our prototype.  Before this call got started we were under the impression that it was going to be just a couple of the executives from the earlier meeting in Canton.  When it actually got started however, it sounded like an entire conference room full of people.  We ended up finding out that the development team in Japan that had written the windows app had been tasked with this same goal earlier and had tried and failed.  I would NOT have wanted to be in that room when the inferior American developers showed it working :).

In the end, the project that we had proven possible was moved offshore where it was morphed into something else entirely.  At least we knew we had done it, and now so do you :).

Wednesday, March 26, 2008

Review: Cleveland C#/VB.NET SIG 3/25

sig_logo Last night was the March meeting of the Cleveland C# / VB.NET SIG.  It was really the February meeting as well since that was delay multiple times due to weather, gotta love Ohio.  This was my first trip to this particular group and I really enjoyed it.

The topic for the night was SQL Server 2008, with Sam Nasr as the presenter.  Since he is also the leader of the SIG it was pretty much all Sam, all the time.  Since SQL Server is still only a CTP much of the information is subject to change but it does look like they have added a bunch of new features that will make development a little easier.  Nothing ground-breaking, but some niceties.  My favorites are some of the new datatypes such as Date, Time and HierarchyId.  I've been wanting them to have a separate Data and Time type for a LONG TIME.

This crowd is a lot more interactive and the group at the BA hosted .NET SIG.  During this presentation there was a lot more discussion, and not just asking and answering with the presenter.  Sometimes a question would be answered by another person (or people) from within the group.  It made for a very good experience. 

Several things come to mind that may cause such a difference in the group dynamics.  First, the average age of this group is probably ten years older than of the other one.  Typically with age come experience and more of a willingness to be "out there" where others can see/hear you, both of which increase the likelihood of speaking up in a crowd. Another factor could be that this crowd and the venue itself were smaller.  The more intimate setting also makes it more comfortable for people to be themselves.  I would name as a third factor that Sam actively tried to engage the crowd, and while that is a needed aspect of interaction, presenters at the other SIG also try this, so although this is a good and needed thing, it doesn't set this group apart. 

One more thing that I would name as different and good is that it felt more informal.  Sam sat during much of the presentation, granted it was partly to reach his laptop, and that help the crowd feel as equals.  Also instead of just jumping into the presentation there was a little bit of a conversational feel to the non-presentation aspects of the night.  Even the presentation felt that way.  I can't point to one thing in particular that made the night different but I think all of the above items contributed to a better experience.

After the presentation was over Sam had a few of us come up and announce Cleveland Day of .NET.  We talked it up and asked people to register and to ask their bosses to sponsor, I think it went over well.  Sam also announced that I'll be doing next month's talk on Silverlight 2.  I guess it's too late to back out now.

And there was an after-party at the Winking Lizard on Rockside again.  There were about six of us there, ok, exactly six, and it was a fun time.  We were talking about how old Mike is :) and what we started programming on and such.  I came to realize that I'm nearing old.  Ick.  The chat was so fun though that before we know it the clock said 11:00PM and we split up to get sleep before work.

So it was a good night, good crowd and I look forward to next month.

Thursday, March 20, 2008

Silverlight 2: User Controls and Other Tips

Silverlight logo So I've started working in Silverlight and am completely blown away by it's power and ease of programming.  I'm not saying that there isn't a learning curve, there is with any new technology, but this one isn't too bad.  To be honest the hardest part of Silverlight 2 is finding information on it.  So with that said I'm going to pass on some of what I've learned over the last couple weeks.

First, the absolute best resource online currently is the forums over at, there are people on there that really know their stuff and are happy to help out others.  You do need to register to use the forums but the same login/password you use at will work here as well.  I also recommend subscribing to the RSS feed of the latest blogs related to Silverlight, tons of good, knowledgeable people get cross-listed there.

So what am I working on?  First, this is what the project sponsors asked for:


As you can see it's boring.  No one cares about look in this application but still, we saw that and immediately thought of a nicer way of doing it.  So we convinced my manager to allow me to build a prototype screen for the feature using Silverlight 2.  He even gave the approval before the beta for Silverlight 2 was released.  So this is what I built: (Disclaimer:  If anyone is an NEC Unified Solutions customer the following screenshot is not in and may never be in a released product)


Look a little nicer eh?  Basically it reads an XML document at runtime that it downloads from an ASP.NET application and then parses that data to create a series of controls.  The controls are one to four Stacks which can contain one or two Modules, each of which contain four Units.  The Stacks, Modules and Units are all specific to the application being built, which happens to be a piece of a PBX management application.  These are represented in the Silverlight application using custom User Controls defined for each piece.  The host application will have business objects that also relate to each of these Silverlight User Control classes.  Each of these user controls (Stack, Module and Unit) are added at runtime based on the downloaded XML document.  There are also more controls at a lower level and a couple of controls that are nothing but forms but for now lets concentrate on those three controls.

I'm not an expert by far, yet, but I have learned quite a bit about them and am very impressed.  In many ways User Controls in Silverlight are much more powerful than their ASP.NET counterparts.  You don't need to worry about state between actions for instance, this one piece took me a little while to figure out but once I did it is powerful indeed.  So here goes, some tips and pointers on Silverlight 2 User Controls.

First there are a couple of things you need to know for the code to make sense, LINQ and the way we're accessing global resources.


The normal XML namespace is not available in Silverlight so if you want to work with XML, you need to learn LINQ.  For me this was probably the hardest part of creating the prototype.  I've created and consumed many a XmlDocument in the past so moving to XDocument instead was a big step, it all looks foreign to me.  I struggled through it though and made it work, though I doubt someone that really knows LINQ to XML would think this code is "pretty".

You will be seeing LINQ to XML used in a few of the below samples but here is a taste:


In the above sample I am parsing the XML displayed below and returning a collection of slot elements.  It's different but makes a lot of sense one you get the hang of it.slotxml 

Accessing Global Resources

Another issue that needed addressed is that of accessing global resources.  This is one area where I had to go to the forums for help.  I'm not sure if the answer I received and thus the approach that I took is the recommended approach, but it does work.  Basically the approach is to take create a singleton class that is loaded during initialization and then can be accessed ad modified from any level.


You will see this object used in some of the later code samples, at least now you will know what you are seeing.  It is all very simple, as is everything in Silverlight, once you figure out how to do it.

Now to the User Controls. 

User Controls are Real Objects

This is something that on the surface sounds like a no-brainer but when you really understand what it means is one of those things that changes the game.  For those of us coming from the web world pretty much all we have ever known is stateless programming.  All of our objects are one-time use and then you load it again if you need it again.  ASP.NET tried to mask this somewhat but the basic fact still remained.

With Silverlight 2 though any properties that you assigned values to still have those values when you next look at that object.  That sentence is a little hard to follow so let me try to explain. 

  1. Add custom properties to your new User Control when you create it.
  2. At runtime assign values to those properties.
  3. When responding to events at a later time those properties still have the value that was assigned.

The above doesn't sound that big but when you have multiple instances of the user control and each instance has unique values assigned to it's properties, the usefulness becomes much more apparent. 

To illustrate this here is a custom User Control I created for the prototype.  This is one of the simplest user controls but it illustrates the points.unitxaml

As you can see it has a very simple presentation, nothing but a square with some text on it, both of which call the same click event handler.  Now for the (truncated) code behind for that same control:rawunitcode

The class is derived from UserControl and defines a number of properties.  It also has a second constructor which initializes those same properties.  Nothing unusual here.  Here is an event handler accessing the properties declared above:rawunitevent

In this sample you can see that we are using the GlobalVariables singleton to obtain a reference to the root element of the XAML.  We then are using some of the values from the active instance of the Unit class to initialize an editable form, even passing in a reference to itself.

Programmatically Adding User Controls

For many dynamic applications you will not know at design-time the exact number of a given piece of your application.  This creates the need to add a given number of said element at run-time.  Enter Programmatically added user controls.

Adding a user control from code is quite simple and very similar to doing the same operation in ASP.NET.  You basically add the new user control to the children collection of some parent object.rawpage

After seeing how the controls are added, the code above of the event handler accessing the assigned properties should make a lot more sense.  You don't have to do any special coding to determine which object was clicked, as it is the actual object you created earlier.  This is a wonderful thing for all of us ASP.NET developers used to loading objects on each postback.

In a real world deployment there may be more logic happening here but this is all it takes to do this.  As you can see it is as simple as creating an instance of the control and then adding it to the parent.  One thing you may have noticed is the use of LINQ to XML, there is a reason for that that I will cover next.

Other Tips

  • Don't put a name on the root level control in your User Control or there can be only a single instance of it in the Silverlight application.
  • Learn LINQ.  I know I already mentioned this one but if you are going to be doing much Silverlight development it is a good thing to know.
  • Use Generic collections.  The Silverlight version of the CLR does not support the "old" collection types like ArrayList so get used to working with the new Generic collections like List<>.  Personally I prefer these anyway.

Final Thoughts:

So this was my first real taste of Silverlight development and all I can say is "WOW".  I already thought that this was going to be a game changer and now I am completely convinced.

This post has become a little longer than I had intended but I feel it is all good information for the ASP.NET developer getting started in Silverlight.  I will be posting more on Silverlight, and I'll also be giving some talks on it that will be announced in the near future.  This is an exciting time to be a web developer!

If you would like more information on this please comment on this post or ask me by another means.  I'll also be giving a presentation on April 22nd for the Cleveland C#/VB.NET Special Interest Group and again on May 17th at Cleveland Day of .NET with this same application as the base for the demos.

kick it on