MVVM Login demo application update, added Silverlight project

This is the same MVVM login demo application, initially made for WPF and we want to add a Silverlight version, but reusing the business logic that we already have. Currently, I’m using Silverlight 2.

  LoginExample-0.0.3.1044.zip (2.2 MiB, 512 hits)

When you start working with Silverlight, you’ll miss a lot of WPF “goodies”. Understandable, since Silverlight is targeting the rich user experience and should stay small. There is no AdornedElementPlaceholder, no Label control, no Style triggers. So, how do we validate since we learned only the trick with binding the IDataErrorInfo to the user interface? Silverlight 3 have support for validation but with throwing exceptions, not with the help of the IDataErrorInfo.

Some Research
There are already some validation libraries for Silverlight, for example:
http://silverlightvalidator.codeplex.com/ (this is an approach similar with ASP.NET Webforms Validators).
That’s mean that if we add a RequiredValidator for the username in the UI, we duplicate the logic of not having username empty from our model. Not good, we want no duplication.

Reuse
To reuse files in Silverlight, just add them as link. For example, we want the class User to be reused in a Silverlight project, too.

Add a new Silverlight class library project, name it LoginExampleDemoBLL.Silverlight. Add the User.cs from LoginExampleDemoBLL project as link.

LoginSilverlightAddAsLink

IDataErrorInfo workaround
IDataErrorInfo is not part of the Silverlight. Luckily, some others had the same problems. I adapt this solution a little to work with my types of view models, which expose the domain object as a property.

Prism (CompositeWPF) help
In order to bind commands to controls, we need the CompositeWPF binaries for Silverlight.
Our XAML for buttons will looks like this:

<Button Content="Login" Margin="5"
               
cal:Click.Command="{Binding Path=LoginCommand}"
               
cal:Click.CommandParameter="{Binding Path=User}"
               
Grid.Column="0" Grid.Row="3" />

So, even if the validation is triggered when control is losing focus, even if we simplify a little our XAML code used in WPF project, is working, is reusable and is very simple as an example.

emipasat posted at 2009-10-18 Category: Development/Programming | Tags: , ,

One Response Leave a comment

  1. #1Zachariah Ashcraft @ 2010-3-7 06:06

    Hey, I came across this post while searching for help with Microsoft Silverlight. I’ve recently switched internet browser from Safari to IE. After the change I seem to have a problem with loading websites that use Microsoft Silverlight. Everytime I go on a website that needs Microsoft Silverlight, my browser does not load and I get a “npctrl.dll” error. I cannot seem to find out how to fix the problem. Any aid getting Microsoft Silverlight to work is very appreciated! Thanks

Leave a Reply

(Ctrl + Enter)