Learning Silverlight #3 - Getting Started

By admin on 12/02/2011

This is a series of posts of my journey learning Silverlight one weekend:



Now I'm all set up I want to immerse myself with as much up-front information as possible, video tutorials are all the rage and providing you have the time can be extremely valuable. Here are the videos I watched and my notes taken whilst watching. These notes detail what I learned and general thoughts as I watched.

These aren't going to mean a great deal alone and I'm not intending to regurgitate the content of the videos but might be useful for others looking for specific content without having to watch each and every video themselves or may be interesting to reference if you watch them yourself. They are also useful for myself as reference when I remember seeing how to do something but can't recall where.

First video - Build your first SL application on Silverlight.net

Build your first Silverlight Application

This video was, as expected, very basic. At some points it went at a frustratingly slow pace however this is understandable due to the very nature of the video. Unfortunately when starting on a new technology, even if you have a lot of relevant experience I find it's best to start from the beginning even though you might already be aware of or assume 90% of what's presented the other 10% is key fundamentals that you will suffer from by not knowing later on.

What was learned

  • Render Transforms (scale/skew) and Effects (dropshadow) applied as child elements of a component.
  • Buttons and textboxes can contain child components, textblock is simply a text output
  • Click handles near edges to "link" dimensions (anchor - grow with page)
  • To enable binding to a class, implement INotifyPropertyChanged, dispatch PropertyChanged event
  • Set DataContext on view to bind to a model (i.e. a class or collection of properties the view is presenting)
  • Binding has TwoWay option to read/write to bindings

This felt a bit too much like "you will be building a website in Silverlight" for my taste. Which, is probably what a lot of people will do... unfortunately. Throughout I found myself needing to think about which bits are useful opposed to those showing the inflexible clunky wizard approaches (add service reference - Boo!).

Overall, it was useful from a xaml perspective and laying the foundations but really what took 35 minutes of time could have been gleaned in less than 10 with a more targeted video. That's not intended as a complaint against the video, it was an excellent tutorial for someone completely new to Silverlight, .Net and general web/application programming but it would be nice to have an option of a whistlestop tour version which highlights the Silverlight specifics rather than the details that are familiar to anyone used to working with Visual Studio.


After a bit of searching around, I couldn't really find a decent next-step on Silverlight.Net, lots of specific tutorials for specific tasks but I'm still looking for more general overview type content to explore the main features and workflow of developing with Silverlight. So... onto the TekPub series.

Tekpub - Mastering Silverlight series

Mastering Silverlight by Christopher Bennage

The first 2 minutes of just the preview gave me a good feeling about these videos. They assumed some knowledge of VS and C# and aims to deliver the fundementals for enterprise app development and lay foundations for WP7, gaming and other more advanced subjects - perfect. In fact just watching the preview I learned a couple of things:

  • Design time DataContext - design view realistic data :)
  • AttachedProperties - brief snippet but enough to give an idea of what this was about and wanting to know more.

So I whipped out the company credit card and bought the series, hopefully it delivers.

Mastering Silverlight 1

  • .net version selected in new project dialogue doesn't matter as SL is its own subset of the framework
  • visual designer (design view) known as "cider"
  • d: is the namespace that references blend
  • Grid component, mouse over top of first row/left of first column and click to add columns/rows
  • Right click, reset layout, all - sensible default layout :)
  • Document outline panel - hierarchy of components, useful for selecting specific component within the displaylist
  • Sizing options: "Fixed" = explicit pixel value, "*" = proptional spread (percentage), "Auto" = large enough to fit child components
  • ItemsControl - list component
  • StackPanel - vertical layout container (VBox equivilent)
  • "Binding" alone will just bind to the current DataContext of the view
  • "Binding MyProperty" will bind to the MyProperty property of the current DataContext
  • d:DataContext allows for a dummy datacontext to use at designtime
  • StringFormat can be applied to binding (e.g. providing a datetime format)
  • PropertyElement syntax: Child object properties and xaml shortcuts
  • MarkupExtensions
  • Button extends Content, which means it can contain other controls
  • Attached Properties: proeprties relevant to a parent control (Grid.Row=1)
  • x:Name vs Name interchangable because xaml parser maps the Name property, but x:Name explicitly is for code-beind access to an element, Name alone is dependant on the element having a Name property in the first place.

Mastering Silverlight 2 - Xaml Layouts

  • Three default layouts out of the box Grid, Canvas and StackPanel
  • Attached property ZIndex - manages z order
  • Can position items outside the bounds of the canvas they are on
  • "Auto" sizes a container to be as small as it can be respecting the properties of it's children
  • * sizes proportionally based on available space and other * sized elements, essentially a percentage but uses an arbitary number and compares against other * sizes (not sure why it doesn't just use percentage)
  • Elements within a grid cell sized with * will respect it's boundaries clipping if it surpasses (e.g. putting a 200px rectangle in a 100px column), ColumnSpan/RowSpan will make the element spread between two rows/cols.
  • "Stretch" horizontal/vertical alignment will respect explicitly set width/height, without will make element fill available space
  • StackPanel has orientation property (vertical/horizontal = VBox/HBox)
  • Panels handle layout in 2 passes: Measure then Arrange
  • Silverlight toolkit (open source extended set of classes beyond packaged Silverlight release) offers DockPanel and WrapPanel
  • Attached properties from external libs (eg. silverlight toolkit) must be referenced with namespace (e.g. toolkit:DockPanel.Dock="Top")
  • WrapPanel same as StackPanel with wrapping

Mastering Silverlight 3 - Controls

  • Border can only have one child, if you need more wrap a grid or other container with the border
  • Viewbox used for scaling/resizing other elements by default will fill available space
  • Default Stretch of Viewbox is "Uniform": constrains proportions
  • Stretch ="None" will not transform image but will clip within the bounds of the Viewbox
  • StretchDirection constrains if an element can be scaled up or down or both (like a min/max width/height)
  • GridSplitter alignment affects which side of the cell it operates on
  • Setting horizontalAlignment to center will essentially use the column as an extension of the splitter resizing the columns either side
  • FileDialogues, can specify file filter or collection of filters.
  • Multiselect returns IEnumerable collection of FileInfo objects.

Mastering Silverlight 4 - Databinding

  • FrameworkElement is base class for all ui controls (similar to UIComponent)
  • Can pass empty string to change notification event to update all bound properties at once (use sparingly to avoid redundant work)

    ...Seems messy having change notification events fired throughout the model...

  • ValueConverters map different types to element properties (e.g. bool to visibility)
  • IVAlueConverter interface
  • UserControl.Resources element creates a dictionary of 'Resource' objects (E.g. ValueConverters)
  • Apply converter in Binding expression by setting the Converter property to the key specified in the Resources dictionary

    ... Feels convoluted for such a seemingly simple task but can see how this might prove useful in more complex scenarios

  • Binding Mode has "OneTime" option - as you'd expect this will only update the property once when the binding is created then ignore subsequent changes
  • Binding is reflected at design time
  • Binding can be applied to other elements using the ElementName property then using the Path to determine which property of the element to use
  • Binding can be applied to a Resource dictionary item

Mastering Silverlight 5 - Building a business app

  • "Quick, not pretty" - eek! This perspective on app development seems ingrained in some within the MS world :(
  • no ADO.Net libs in SL
  • Project type "Silverlight Business Application" gives a default applciation shell with views, helpers, a login form and a web project offering a wcf ria services implementation
  • GlobalSurpressions.cs turns off code analysis routines
  • .xap is the file for a compiled silverlight project used to host in a web application
  • Shared code between SL and web app, compiled for both but doesn't need separate assembly

    ... not sure if i like or not, good for DTOs perhaps but easy to abuse and cross concerns over boundaries

  • RIA Services offers authentication service and registration service out of the box
  • RIA services uses entity framework by default can use whichever ORM you wish
  • In SL RIA services offers a "domain context"

    ...unsurprisingly I do not like the look of RIA Services as it just encourages too much crossed roles between front-end and backend. The frontnend has too much knowledge of the service layer's responsibilities.

  • DataPage object allows setting of page size and handles paging automatically

    ...Example of why bad, the client application in the example triggered an EF error by changing some client code trying to add result paging!? This is absolutely a server-side task and not something the client should be instigating. On first encounters WCF RIA Services extremely tightly couples your client to your service implementations. The whole integration of it suggests the same developer is doing the front-end and back-end development. In all but the rarest of cases this will lead to a poor experience. The whole discussion is worthy of it's own post, fortunately someone has done that for me :)

Mastering Silverlight 6 - Building a business app part 2

  • DataForm control - builds a form based on a model

    ...again don't like the sound of this as someone should be giving such interactions more considered thought and attention to detail.

  • DataForm binds to a collection allowing iterating through the collection editing one record at a time.

    ... here, the model is determining UI elements, sometimes this makes sense (e.g. required fields) but again, this is something that should be given due consideration at the interface/ux level.

  • Navigation framework allows navigating an app based on URI, solves browser back button issues, deep linking and refreshing

    ...Services being declared in views doesn't feel right, probably this way for demonstration purposes, hope to read some alternative approaches so views aren't bound directly to web services.

I skipped through the last video as it was a bit too focused on building a form-based application in an overly prescribed manner. This may be useful at some point when experimenting with different approaches for creating datagrid and forms driven apps but I'm happy with what I obtained from the previous videos for now.

End of day 1 Summary

Well throughout the day I have watched about 4 hours worth of Silverlight content and have got a lot from it. I would definitely recommend the Tekpub series to anyone else wanting to get up to speed. The author of the videos (Christopher Bennage) knows his stuff and presents the content very clearly and at a good pace. After the first four videos I was really impressed with Silverlight and the Visual Studio integration, it's come a long long way since the last time I investigated (pre v2). I was somewhat disappointed in the final two videos but that's not an issue with the actual tutorials but rather the technology they presented. WCF RIA Services are not for me, they are far to reminiscent of the perils of wizards and drag and drop RAD that are great for product demos and getting new developers up and running quickly but very quickly fall flat as soon as some levels of complexity are introduced into a system. I will post more on this another time as that's not the intended focus of this exercise :).

Where Next?

I'm keen to actually start coding so will have a play around producing a trivial sample application next. I also want to spend some time learning Blend so will look for some good video tutorials on that. I also want to spend some time researching some best-practices (such as MVVM and similar) and look into alternatives to RIA Services. Finally, if I have time I'd like to take a look at WP7 as I'm getting sucked into the hype around that at the moment. That's enough for today though I feel I've learned a lot for one evening and one day, we'll see what tomorrow brings...