Go back

Øredev 2010 - Day 1 - Tim Jeanes

Øredev - not the plushest conference venue

Øredev - the only conference I've known to be held in a converted slaughterhouse.

A bootcamp in Silverlight - Jeff Wilcox, Tim Heuer Einar Ingebrigtsen

I've tinkered a little with Silverlight at a basic level in the past, but not really used it in anger. This intensive session took us from the very basics through to cover around 99% of Silverlight within a single day, so I've only blogged the points that stood our for me.

RoutedEvents - events that a parent can also receive (Focus, LostFocus, etc.) - these bubble all the way to the root of the VisualTree. Can set the Handled property of EventArgs to stop bubbling, though this isn't available on all such events.. OriginalSource holds information about the source of the event.

ContentControl - the base class for classes that have content (e.g. Button, TextBlock).

ItemsControl - the base class for controls that have items (just as lists, etc.)

HierarchicalDataTemplate - the base class for hierarchical structures such as TreeView.

Animation - all done through storyboards. Any property can be animated - not just visual things (e.g. you can animate the value of a string). In all animations you specify keyframes and Silverlight interpolates between them using the specified easing. nimation storyboards can be started in code, or via triggers in the xaml.

Sample Data - This is a powerful feature of Blend. You can easily specify a data structure (Customer has FirstName, LastName, Photo, etc.) and mock data will be generated for you - lots of lorem ipsum-like text and arbitrary images in our case. This makes it really easy to get started with a data-driven app that you can even show to a customer right away.

DependencyObject and DependencyProperty - When making your own controls, can define DependencyProperties. This enables properties to be animated; it also provides two-way dependency, so the view is updated with changes made in code as well as the code being notified of changes made to the value via changes made in the view.. This points to the actual property whose value is to be changed. You can also specify metadata such as the default value and a callback method that will be called whenever the value is changed by some action in the view.

This makes for quite a bit of extra typing and non-refactorable code (the actual property name is specified as a string in the DependencyProperty definition, for example). It's worth using a generic wrapper that will register the DependencyProperty for you. Einar's set up some sample code of how to do this, available here: [.]

Resources - As well and application-wide resources, all UIElements can have resources available for their scope. Though resources are usually used for styling, resources can be any objects. Only static resources are available in Silverlight - WPF's dynamic resources can't be used here.

Styling - this goes way beyond the powers of CSS: any property can be styled - including ones that aren't visual. Styles can be inherited from one another using the BasedOn keyword.

Visual States - Controls can have different states (e.g. MouseOver, Focused, etc.). Using the VisualStateManager you can set styles for different states and the animations between them.

Markup extensions - These allow you to use pseudo-values for properties, such as {Binding}, {StaticResource}, etc.

DataContext - All UIElements have a DataContext, which is inherited from its parent by default.

Binding data - Different modes are available: OneWay (the default, which only takes data from the object to the view), TwoWay (which sends data back), OneTime (which reads the data once at start-up and never updates).

IValueConverter can convert string values in the model to a different value that is used on the view. This can also work both ways.

String formats, null and fallback values can also be defined.

Attached Dependency Properties - these allow you add your own properties to the existing properties on a DependencyObject. You can see these in action in the Grid control: it adds the Grid.Row and Grid.Column properties to child elements that appear within the Grid.

Element-to-element binding - One element can bind to the properties of another element. This can be useful, for example, in list/ detail binding.

Silverlight promotes using an asynchronous model, so INotifyPropertyChanging and INotifyPropertyChanged interfaces are used to mark the beginning and end of the async operation.

TypeConverter - Everything in xaml is a string, so these are used to convert between objects and strings between the view and the model. They are defined through attributes on types or on DependencyProperties.

Offline support - Isolated storage is available for storing data locally. Silverlight can also be run out-of-browser. The API provides methods to detect network availability and events are raised when the network availability changes.

ChildWindow - an easy way to create modal controls. Watch out though - it's easy to tab from this control back to the parent. It's a bit tricky to work around this bug.

DispatchTimer - a timer for running code at a predefined point in time. This runs on its own thread so you need to use the Dispatcher to execute operations on the UI thread.

Local file access - you can't access any old file on the hard drive; however OpenFileDialog and SaveFileDialog are available to ask the user to access files.

Local Connection API - enables you to communicate between multiple Silverlight instances on a single HTML page.

Fonts - Silverlight has its own font engine to break the dependency on the OS, thus ensuring it will render the same everywhere. Nine fonts are included in the runtime, but you can embed your own or access certain ones that you can (hopefully) assume exist in the OS. TrueType fonts are supported.

WriteableBitmap - this enables you to create your own bitmaps dynamically at runtime. It's surprisingly performant.

Printing - Any UIElement can be sent to the printer. However, it's pretty limited. It doesn't always look great as it's pixel-based. There's no paging support - anything you print will come out on a single page.

WCF Services - Adding a Silverlight-enabled web service to your server application really adds a regular WCF service - but it also sets everything up for Silverlight consumption in the web.config.

MVVM - the Model - View - View Model model works really well with Silverlight. The View Model is where you spend most of your development time in Silverlight. It should contain business logic relevant to the frontend, but not view-aware. It typically connects to the server through services. In terms of state, it contains as much model-like information that the view needs, with the application logic state.

Though MVVM costs a bit in terms of the initial learning curve and early development time (though it's less uncomfortable if you're already used to ASP.NET MVC), it's worth it for the separation of concerns and code testability and maintainability. It also makes it a lot easier to make drastic changes to the UI without breaking the application.

Silverlight toolkit - Available at silverlight.codeplex.com, this is an open source library of useful Silverlight components that may one day make their way into the core Silverlight library. It includes tools for WP7 development.

Got a project? Let's work together

Compsoft is a remote working company except for Thursdays when we work together from the Alton Maltings in Alton, Hampshire (UK). Our registered address is detailed below. We'd be delighted to have either a virtual chat with you or to make arrangements to meet in person.