Page view counter

Jesse Liberty - Silverlight Geek

More Signal Less Noise

Throwing Together An Application

Creating a Quick and Dirty App in Silverlight

 There comes a point, usually within 3-6 months after I commit to a new technology, when it becomes my platform of choice for throwing together quick applications… when I just find it easier to think in terms of the new technology than one of the old.

I also find that it usually comes in two stages: first, it is easier to use the new technology for small illustrative programs, then it is easier for creating quick and dirty utilities, apps, whatever it is I need to get done.

Today I had my first such opportunity when my daughter needed a stop watch for her Mac. Rather than looking for one (I'm sure there are hundreds) it just seemed easier to throw it together using Silverlight. 

Total development time, including thinking, napkin design, layout in Blend, coding, debugging, and testing on Windows and Mac… 20 minutes. Not too bad.

Rachel timer on mac

One of the things I enjoyed about this project was that it reminded me how very cross-platform Silverlight really is. This was, of course, written and tested on Vista, and yet, when I ran it on the Mac, it looked just like a Mac program should look. I had to go back and run it on Vista to remind myself that it started out life looking like a Vista program!

Rachel timer on vista

That was kind of neat.

Start In Expression

Creating the program itself was painfully simple. I started out in Blend, and created a grid, then filled in the heading with a Textblock, added a row with three buttons, then four more text blocks. 

TimerProgramInBlend

 

Once you get over the idea that Blend "really isn't for our kind" – it is a gas. I make a point of using Comic Sans MS as I'm told it is a clear signal to any designer that I don't have a clue what I'm doing design-wise; it's the design equivalent of wearing a button that says "what is an object, anyway?"

…Proceed To Visual Studio

With the UI in place, and all the little UI objects named, I flipped over to Visual Studio to write the code to support the timer.

I won't walk you through every detail (unless you need the sleep) but briefly… I gave each button its own event handler (I'd probably consolidate this to a single handler if the program were any larger).  Taking them easy to harder…

The Pause button calls Pause( true) which disables the Pause button (so you can't press it twice), and sets a paused flag.

The Stop button disables both the pause and the stop button and sets the started flag to false and turns off the timer (timer is explained in just a moment). The net effect is to stop everything but leave the current elapsed time showing.

The start button has all the fun. It enables the Pause button and sets the startTime member variable to now (we keep track of two times, the time since the start button was first pressed, and the time since it was most recently pressed). It then calls a helper method named Start.

The helper method checks to see if this is the first time the start button has been pressed. If so it records the time (we use this for the total elapsed time) and enables the stop button and we reset the elapsed time and start up the timer.

The timer uses a System.Windows.ThreadingDispatcherTimer as explained here.

The start code (combined and cut down) looks like this:

   1: void startButton_Click( object sender, RoutedEventArgs e )
   2: {
   3:    // un-pause
   4:    Pause( false );  
   5:    // start a new elapsed time
   6:    startTime = DateTime.Now;  
   7:    Start();
   8: }
   9:  
  10: private void Start()
  11: {
  12:    // if first time started
  13:    if ( started == false )
  14:    {
  15:       started = true;
  16:       stopButton.IsEnabled = true;
  17:       firstStartTime = startTime;
  18:       ResetElapsed();
  19:       StartTimer();
  20:    }
  21: }
  22:  
  23: public void StartTimer()
  24: {
  25:     // set up timer with event handler
  26:     timer = new System.Windows.Threading.DispatcherTimer();
  27:     timer.Interval = new TimeSpan(0, 0, 0, 0, 100); 
  28:     timer.Tick += new EventHandler(TickTock);
  29:     timer.Start();
  30: }
  31:  
  32:  
  33: // event handler for timer (10 times/second)
  34: public void TickTock( object o, EventArgs sender )
  35: {
  36:    if ( !started )
  37:       return;
  38:  
  39:    totalElapsed = DateTime.Now - firstStartTime;
  40:    TimeSpan elapsed = notPausedElapsed;
  41:  
  42:    if ( !paused )
  43:    {
  44:       TimeSpan temp = DateTime.Now - startTime;
  45:       elapsed += temp;
  46:    }
  47:  
  48:    // display paused and total time
  49:    time.Text = elapsed.Hours.ToString() + ":" + elapsed.Minutes.ToString() + ":" + elapsed.Seconds.ToString();
  50:    totalTime.Text = totalElapsed.Hours.ToString() + ":" + totalElapsed.Minutes.ToString() + ":" + totalElapsed.Seconds.ToString();
  51: }

As you can see, no rocket-science; and very familiar; we've all done it a zillion times. But it works, and it works on the client, cross-browser and cross-platform. You can stream it or you can just drop the .xap and html file right on the machine so you don't have to be on line to use it. Sweet.

Here's the source code for this post: RachelTimer1.zip

 

Comments

Joseph G. said:

Nicely done.

Well. what amaze me about silverlight not only its cross-platform, cross-browser support but how silverlight team has managed to squize a scaled down version of the CLR and the BCL and make it workable on the client-side.

# September 15, 2008 1:32 AM

Throwing Together An Application said:

Pingback from  Throwing Together An Application

# September 15, 2008 1:39 AM

Community Blogs said:

Martin Mihaylov on the DataGrid, Bart Czernicki on Game Concepts, James Bacon on an update to Mashooo

# September 15, 2008 3:20 PM

Mark Washabaugh said:

The market is tight, trying to borrow money is so frigging hard these days. Clients are less likely to spend money, no promissing cash flow is see-able on the horizon.

You rest all your hopes on a new technology that will rise you from the ashes of a dismal economy. You're promised real life apps will be provided so you can learn more how to use this new technology to better your business. What do you receive weeks after the promise? How to create a stop watch.

Jesse, I think you've become soooo comfortable in your every two weeks paycheck job that you forgot what it means to be self employed and to be constantly leaning forward to keep your business a going concern.

Thank you so much for this stop watche xample. I am sure I will be able to pay my mortgage and feed my family as a result of it. Who needs real app tutorials when you have the ability to make a stop watch?

# September 15, 2008 8:54 PM

simonjohnroberts said:

Hey thanks for the tut.

Ignore mr grumpy,

There are lots of good in depth tuts out there.

This was a good little one.

Sigh

Si

# September 16, 2008 12:16 AM

Kevmeister said:

To Mark:

Yes, the stopwatch app was trivial. I think that's usually what the term "quick-and-dirty" means. Nonetheless it demonstrates a number of behaviours. What you seemingly fail to give consideration to is that people learning Silverlight exist on a knowledge continuum ranging from complete beginners to seasoned and professional developers. Clearly you are of the opinion that if it's too trivial for you it's obviously of no benefit to anyone. Bzzt.

So get off your high horse and stop whinging that the example in question is too trivial for you. Quick-and-dirty Apps *are* trivial.

Instead, perhaps look elsewhere and broaden your horizons. There are numerous other contributors to the Silverlight space that you can learn from, presenting simple and complex examples.

Perhaps you think Jesse should craft a full ERP system to rival SAP and then give us a 5000 page monologue on how it was built and every technical detail that went into its design?

What you refer to as a "full App tutorial" is often just a bunch of small details packed into one example. Depending on your preferred mode of learning, it can be a curse or a blessing. Smaller, but more targeted examples are often better at focusing on one aspect of the technology, and eliminating the surrounding "noise".

# September 16, 2008 12:20 AM