Silverlight


I am presenting on Windows Phone 7 at the next SLUGUK meeting – should be fun.  Rich Griffin will be joining me and we are expecting to have a real device or two to show off.

Details are here: http://consultingblogs.emc.com/markmann/archive/2010/07/07/silverlight-uk-user-group-july-2010-agenda.aspx 

Hope you can make it!

Cheers

Ian

bbits are giving away a free ticket for the Silverlight Masterclasses in June

To enter the raffle all you need to do is use the exact text between these lines in your blog post, including all the links, then email Ian@bbits.co.uk with the url to the blog entry.  You can chose any of the events listed on the site.

The draw will be made on June 1st and the winner notified by email and via this blog.

Good Luck!

————– TEXT STARTS HERE

The Silverlight Tour comes to the UK – and it’s called the Masterclass!

This 3 day hands-on training with both designer and developer tracks looks awesome and (uniquely) has two expert trainers per course. 

Currently scheduled in London, Manchester, and the Midlands for June, all courses also come with the chance to win an xbox 360, and Silverlight Spy licences!

Early bird discount of £100 if you book in May, and if you are a member of #SLUGUK or #nxtgenug there are additional discounts to be had.

Full Details are here: http://silverlightmasterclass.net

In addition bbits are holding a raffle for a free ticket for the masterclass. To be eligible to win the ticket (worth £1095!) you MUST paste this text, including all links, into your blog and email Ian@bbits.co.uk with the url to the blog entry.  The draw will be made on June 1st and the winner informed by email and on http://silverlightmasterclass.net

————- TEXT ENDS

Cheers

Ian

Behaviours are growing in importance within Blend – especially for developing Silverlight and WPF apps using the MVVM pattern.  In fact they are becoming so useful we have to wonder when they will be made available in the editor of VS 2010.

A couple you should definitely look at, if you haven’t already done so when using MVVM, are the CallMethodAction and InvokeCommandAction that make it easy to wire up a Method or Command in your ViewModel using the Blend editor.

However a new one that now ships in box with Blend 4 offers something a little more interesting and perhaps less obvious.  This is the DataStateBehavior which allows you to change to a Control State depending on a property of your ViewModel.  This struck me as an easy way to provide an animation when your ViewModel is doing some async work.

Lets take a simple example.  First the ViewModel.  This has a single method called DoWork that simulates a few seconds of work (e.g. calling an async service) and a property called IsWorking that can be set to true whenever the ViewModel is doing some prolonged work.  One way to handle this in a more complex scenarios when you could possible have multiple async calls happening is to have a counter that you increment whenever you start some work and the decrement when you stop work.  Whenever the counter is positive then you should display your “loading animation” which we’ll achieve through the DataStateBehavior.  Here it is:

   1: public class MainPageViewModel : INotifyPropertyChanged

   2: {

   3:  

   4:     private bool _isWorking;

   5:     public bool IsWorking

   6:     {

   7:         get

   8:         {

   9:             return _isWorking;

  10:         }

  11:         set

  12:         {

  13:             _isWorking = value;

  14:             RaisePropertyChanged("IsWorking");

  15:         }

  16:     }

  17:  

  18:     private int _workCount;

  19:     private int WorkCount

  20:     {

  21:         get

  22:         {

  23:             return _workCount;

  24:         }

  25:         set

  26:         {

  27:             if (value < 0) value = 0;

  28:             _workCount = value;

  29:             IsWorking = _workCount > 0;

  30:         }

  31:     }

  32:  

  33:     public void DoWork()

  34:     {

  35:         //simulate some work

  36:         WorkCount++;

  37:         DispatcherTimer timer = new DispatcherTimer();

  38:         timer.Interval = TimeSpan.FromSeconds(3);

  39:         timer.Start();

  40:         timer.Tick += (sender, args) =>

  41:         {

  42:             WorkCount--;

  43:             timer.Stop();

  44:         };

  45:         

  46:     }

  47:  

  48:     public void RaisePropertyChanged(string propertyName)

  49:     {

  50:         if (PropertyChanged != null)

  51:         {

  52:             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

  53:         }

  54:     }

  55:  

  56:     public event PropertyChangedEventHandler PropertyChanged;

  57: }

 

In Blend I first set the DataContext for the User Control to this ViewModel

image

Which looks like this in Xaml and will instantiate the ViewModel and set it as the DataContext for this View (UserControl)

image

I drew a big button on the artboard and then created a couple of states called “Working” and “NotWorking”.  I had a bit of fun setting some effects and animations for the state transitions.  In particular I made sure the animation for the Working state was set to auto-reverse and repeat forever.

image

Now comes the fun bit by using Blend behaviours to wire up the click event on the button to the DoWork method in the ViewModel and then to use the DataStateBehavior to change between the Working and NoWorking states based on the IsWorking property on the ViewModel.

First to wire up the Do Work Method (Note there is also an InvokeCommandAction you can use if you have a command on your ViewModel rather than a simple method I have created here).  To do this you simple drag the CallMethodAction from the Behaviors section in the assets panel and set the properties as shown below:

image

To wire up the DataStateBehavior the process is similar – drag it from the behaviour from the assets panel and set the properties as shown:

image

This looks like this in XAML:

image

And that’s it!  Running the app and clicking the button will run your method in your ViewModel and dispay your Working state.  Once the work is complete in the ViewModel the control returns to the NotWorking state. 

This is nice because:

  • The ViewModel knows nothing of the View and so is more easily testable.
  • Everything can be wired up in Blend – no code to write for a designer.
  • The Xaml is actually nice and straight forward to, and easy to understand.

I would like to see Behaviours in the VS2010 too, because I think there is definitely a case for developers taking advantage of this sort of thing, though it wouldn’t take much for them to type in the Xaml directly.

Example project is available from here: http://cid-fb8b852ef1ab0b35.skydrive.live.com/self.aspx/SampleCode/SimpleMVVMLoadingAnimation.zip 

Cheers

Ian

P.S. Don’t forget our Silverlight Masterclass is now touring the UK:  Http://silverlightmasterclass.net – book early!

I am chuffed to be talking at the user group meeting tonight, along with Ian Smith.

Details here

I will be talking about two things :

Silverlightpulse.net

This was the micro app I created before the Mix 09 conference in anticipation for the Silverlight 3 announcements and why I was originally invited to talk at the user group meeting (it’s a bit of a long story as to why it has taken so long for that to actually materialize – filled with stories of product launches and twisted ankles!). 

I still think this is a very interesting app though, and has some potential that I have yet to realize.  Tonight I will be going into why I think that and asking: “is this a worthwhile community project and who wants to be involved?”

Using RIA Services with Silverlight

To me RIA services offer a prescriptive pattern that will be useful for a majority of projects.  ScottGu called out 80% as the take-up figure when he talked about it recently and John Papa said in a recent tweet “I think RIA Services could be the best thing to happen to Silverlight LOB apps”.  I agree, though some may not entirely.  I only have an hour in total this evening but I hope to give a flavour of RIA services and what it can bring to the party.

Hope to see you there!

Cheers

Ian

During a recent course with Stan James  we discussed how Silverlight 3 could be used with duplex services to push data from the server to the clients; I wanted to produce a simple example of how to do this.  Pushing data this way has a number of benefits:

  • You reduce the load on the server; instead of multiple clients polling the server periodically, we send the data from the server to the clients as and when we want to.
  • We can eliminate or reduce data lag in time sensitive applications – so as soon as we have the data available we can push it to Silverlight to react to.  This can be very important for applications that need to show data changes as they happen, such as trading systems or perhaps live statistics from sports events such as Formula 1.
  • All clients receive updates at the same time.

There is a nice sample application from Mix09 available here which creates a chat application as well as an example of a server push.  However I felt a simpler example would have a benefit to many trying to get to grips with this, together with a bit more explanation.  We will however use generic DuplexService class that is created in that project (it would make sense for a class like this to be available in the framework, or at least a template within Visual studio)

What our application will do

We want a simple example, and that is what we will get.  The server will periodically push a “LiveData” message to any Silverlight client that has connected, and the client will display it.  I am not concerned how the data gets to the server here, only how to send it out to the client once we have it. 

How the project fits together

image

Creating the Sample

Creating the Project

First create a new Silverlight project; let’s call it DuplexExample – ensure you include a Asp.Net Web Application with the default name (DuplexExample.Web) .  In the web site add a reference to the System.ServiceModel.PollingDuplex (make sure you get the correct one – the v3.0 one in the Server folder.)

image

And in the Silverlight project also add a reference to System.ServiceModel.PollingDuplex (but this time in the Client folder):

 image

Download the Duplex sample from http://code.msdn.microsoft.com/silverlightws/Release/ProjectReleases.aspx?ReleaseId=2401, extract it and grab the DuplexService.cs file and drop it into your web site.

Creating the Service

Now we could add a Wcf service using Visual Studio templates, but that would actually create a lot of stuff we don’t need which we would just have to delete, so instead just add a text file but call it LiveDataService.svc . This will give us a blank file into which you can add the following:

<%@ ServiceHost
       
Language=”C#”
       
Debug=”true”
       
Factory=”DuplexExample.Web.LiveDataServiceFactory”
       
Service=”DuplexExample.Web.LiveDataService”
        
%>

 

The only difference here to the contents of a normal svc file is the fact that there is no CodeBehind value specified and we have explicitly specified a Factory class.  (For more details on these options see here: http://msdn.microsoft.com/en-us/library/aa967286.aspx) Since we have specified a LiveDataService for our service and LiveDataServiceFactory class that will be used to instantiate the factory service, we will need to go and create these. 

But  before we can do that we need a DataContract class called LiveDataMessage – this is the simple class that our service will send to Silverlight.

[DataContract]
public class LiveDataMessage: DuplexMessage
{
    [DataMember]
    public int Value { get; set; }
    [DataMember]
    public string Description { get; set; }
}

Ok so now we can create our “LiveDataService” – add a class to your web site called LiveDataService.cs  and add the following code.  This is fairly simple – we are using a Timer to send a message to all clients every 5 seconds:
 

using System;
usingMicrosoft.Silverlight.Cdf.Samples.Duplex;
usingSystem.Threading;

namespace DuplexExample.Web
{
    public class LiveDataService :DuplexService
  
{
        Timer liveDataTimer;

        public LiveDataService()
        {
            //Set up a an update every 5 seconds
          
this.liveDataTimer = newTimer(newTimerCallback(LiveDataUpdate),null, 0, 5000);
        }

        void LiveDataUpdate(objecto)
        {
            LiveDataMessage liveDataMessage = newLiveDataMessage()
            {
                Description = “Live Data at “+ DateTime.Now.ToLongTimeString(),
                Value = newRandom().Next(0, 100)
            };
            PushToAllClients(liveDataMessage);
        }
    }
}

The DuplexService Class

You will notice that our LiveDataService inherits from the DuplexService we added to our project – this provides all the plumbing work for the duplex service:

image

Crucially this class is also marked with the following attribute, meaning our service can keep track of clients that have connected, because it stays in memory and is not recycled’:

image

Ok – so the DuplexService provides plenty of goodness for free, though there is one thing we need to alter in this file – you may have noticed already that it is not compiling.  Down at the bottom you will see a list of KnownTypes.  These related to the sample project you downloaded and not what we want.  We only have one specific KnownType (apart from the Connect and Disconnect) called LiveDataMessage. 

So you need to make the following change to the DuplexService.cs file (at the bottom) :

image

The LiveDataServiceFactory class

Finally we need to add the LiveDataServiceFactory class  – which inherits from the DataServiceFactory class in the DuplexService.cs file (and in fact that’s all it will do)

namespace DuplexExample.Web
{
    public class LiveDataServiceFactory : DuplexServiceFactory<LiveDataService> {}

}

If you go and look at the DuplexServiceFactory we are inheriting from (again in the DuplexService.cs file) you will see that it provides the configuration for the service with a custom binding (this is an alternative to providing configuration for doing this)

image

The Silverlight Project

Make sure you can build your solution, then add a Service Reference from the Silverlight project to the LiveDataService you have created:

image

I created a very simple UI – just a button and a ListBox shown below:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="225"/>
    </Grid.RowDefinitions>
    <Button Content="Connect"             Click="ButtonConnect_Click"             x:Name="ButtonConnect" Margin="10"></Button>
    <ListBox Grid.Row="1"             ScrollViewer.VerticalScrollBarVisibility="Visible"              x:Name="ListBox1"></ListBox>
</Grid>

image

Here is all the code in MainPage.Xaml.cs.  Take a look at it here then we’ll discuss it:

public partial class MainPage : UserControl
{
    DuplexServiceClient receiver;
    ObservableCollection<string> liveDataMessages =                                 new ObservableCollection<string>();
    CustomBinding binding = new CustomBinding(
                                new PollingDuplexBindingElement(),
                                new BinaryMessageEncodingBindingElement(),
                                new HttpTransportBindingElement()
                                );

    public MainPage()
    {
        InitializeComponent();

        ListBox1.ItemsSource = liveDataMessages;

        //make sure the endpoint address is correct
        receiver = new DuplexServiceClient(binding,                  new EndpointAddress("http://localhost:6719/LiveDataService.svc"));
        receiver.SendToClientReceived += (sender, e) =>
            {
                if (e.msg is LiveDataMessage)
                {
                    LiveDataMessage msg = (LiveDataMessage)e.msg;
                    liveDataMessages.Add(string.Format("{0}.                      Value = {1}", msg.Description, msg.Value));
                }
            };
    }

    bool connected = false;
    private void ButtonConnect_Click(object sender, RoutedEventArgs e)
    {
        if (connected)
        {
            Disconnect();
        }
        else
        {
            Connect();
        }
    }

    private void Connect()
    {
        ButtonConnect.Content = "Connecting...";
        receiver.SendToServiceCompleted += (sender, e) =>
        {
            ButtonConnect.Content = "Connected (click to disconnect)";
            connected = true;
        };
        receiver.SendToServiceAsync(new ConnectMessage());
    }

    private void Disconnect()
    {
        ButtonConnect.Content = "Disconnecting...";
        receiver.SendToServiceCompleted += (sender, e) =>
            {
                ButtonConnect.Content = "Disconnected (click to connect)";
                connected = false;
            };
        receiver.SendToServiceAsync(new DisconnectMessage());
    }

}

At the top of this code, we declare 3 variables: the DuplexServiceClient,a  CustomBinding (which matched the binding of the service we created on the server) and an ObservableCollection to store the messages we get from the server.  We instantiate the DuplexServiceClient in the class constructor using the custom binding and and endpoint (make sure this is the correct address for your service) and wire up the SendToClientReceived event which is where we add the messages we get from the server to the observableCollection (which in turn shows them in the ListBox)

However we also need to provide a Connect method (which sends a ConnectMessage to the server) – this is to essentially to let the Duplex service on the server know we are here, and allow it to keep track us via a Session ID.  You can see this in DuplexService.cs file as shown below (line 133):

image

The disconnect method does the reverse by sending a DisconnectMessage to the server which reacts like this (removing the client from it’s tracked session id’s)

image

Testing Your Applicationimage

Press F5 to run the application, then click the Connect  button  – you will see messages sent to Silverlight from the Server every 5 seconds.  To make it more interesting, start a second browser instance and run the same application and connect that too.  You will see both clients being updated at the exactly the same time.

 

Hope this is of use.

You can download the source code from here: http://cid-fb8b852ef1ab0b35.skydrive.live.com/self.aspx/SampleCode/DuplexExample.zip

Cheers

Ian

Technorati Tags: ,,

Silverlight for Business has been hosted over on Live Spaces for the last 6 months.  However there are quite a few issues with using that platform which don’t look like been fixed any time soon.  So for that reason I am moving it here.

All the old posts are still available with the original links, and I have summarised them here:

By the way, I have a stack of new posts on the way that I have been building up, so stay tuned.

Cheers

Ian