My first Silverlight 2.0 Example

image

This morning I had my first exposure to Silverlight 2.  I like it.  I had a couple of issues deploying though on my server due to my server not being configured correctly.  So here are a couple of things to make sure of before attempting the application.  I have done a small app to which simply displays some of the DateTime controls in Silverlight with a nice slider example.

 

I have IIS 6 so based on that

  1. Add two mime types to your website in IIS

 

image

2. Make sure the ClientBin folder is in the root of your site along with your bin folder obviously.

This should make it work then.  I did not have to rename the xap to zip and what not, like other blogs have posted.

The example application

Download : http://www.andrewrea.co.uk/Silverlight/Example1/SilverlightApplication1.zip

Example: http://andrewrea.co.uk/Silverlight/Example1/SilverlightApplication1TestPage.aspx

 

For my first exposure to Silverlight, I wanted to choose a few controls and get them on the screen.  I chose:

  • A DateTime picker
  • A Calendar
  • A Slider
  • A Toggle Button
    • I did not do anything with this, just added it through curiosity.  Nice effect.

For the DateTime picker I subscribed to the DateSelected event and in this block I set the calendar to have the selected and displayed date equal to what I had selected.

        private void DatePicker_DateSelected(object sender, DatePickerDateChangedEventArgs e)
        {
            Calendar c = this.FindName("Calendar1") as Calendar;
            c.SelectedDate = e.AddedDate;
            c.DisplayDate = (DateTime)e.AddedDate;
        }

The second event I subscribed to was the ValueChanged event of the slider control.  I used this to set the day of the selected month of the calendar.  I put in a try catch block here to account for months whose number of days differ.

The XAML markup for my slider

<Slider Minimum="1" Maximum="31" Width="200"  VerticalAlignment="Top" HorizontalAlignment="Left" Margin="25" ValueChanged="Slider_ValueChanged"></Slider>

And the code for the event

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Calendar c = this.FindName("Calendar1") as Calendar;
            if (!c.SelectedDate.HasValue)
                c.SelectedDate = (DateTime)DateTime.Today;

            DateTime selectedDate = (DateTime)c.SelectedDate;

            try
            {
                c.SelectedDate = new DateTime(selectedDate.Year, selectedDate.Month, (int)e.NewValue);
            }
            catch (ArgumentOutOfRangeException)
            {

            }
        }

I was a bit stumped at first, thinking how do I reference the controls on the screen.  I achieved using the above.  If anyone is interested I bought the following books to help me along my way:

Programming WPF Chris Sells & Ian Griffiths publisher O'REILLY

Programming WCF Services Juval Lowy publisher O'REILLY

Published Wednesday, March 19, 2008 11:22 AM by REA_ANDREW

Comments

# re: My first Silverlight 2.0 Example

Wednesday, March 19, 2008 11:43 AM by Michael Washington

Well it looks like you are off to a good start :)

# re: My first Silverlight 2.0 Example

Wednesday, March 19, 2008 12:00 PM by REA_ANDREW

Thanks for the comment :-)

# re: My first Silverlight 2.0 Example

Tuesday, July 15, 2008 5:59 PM by Rahul Gangwar

It is old now. Build it on SL 2.0 Beta2. Most of the people are not able to run the demo.

Leave a Comment

(required) 
(required) 
(optional)
(required)