Updating the Simple Silverlight Countdown Blog badge

image When I was preparing for RIAPalooza, I created and blogged about Creating a Simple Silverlight Countdown Blog badge.

It was great but I wanted to updated it for the next event and realized that recompiling it was not really a user friendly option. The answer is that I updated the widget to have a level of parameterization so that I could easily update it without having to recode everything.

*Update – Source Code

The good news is that there’s a mechanism called InitParams that allows you to pass in initialization parameters on startup. I’m not wild about the mechanism itself as it’s just a key-value pair string and not individual parameters but it works. The alternative is to do something where you write some javascript that will call into the Silverlight object and set parameters. While that works, it wasn’t my first choice because you have to worry about the order of events, exposing objects as scriptable and so on.

<object data="data:application/x-silverlight-2," 
type="application/x-silverlight-2" width="100%" height="100%"> . . . <param name="initParams"
value="eventDate=01/13/2010,registrationUrl=www.codemash.com,
registerText=Check it out!,startImageUrl=logo_codemash_2010.jpg,
endImageUrl=logo_codemash_2009.jpg" /> . . .
</object>

Notice that there are multiple parameters here separated by commas. The first one is a date and the second one is a URL and so on.

To access that init parameter in the startup you need to open the App class found in App.xaml.cs and in any method use the Host object’s InitParams list.

string fieldValue = this.Host.InitParams[key];

For a lot of reasons, I wrap that up in a method as follows:

private string GetInitParam(string key)
{
    string fieldValue = "";
    if (this.Host.InitParams.ContainsKey(key))
    {
        fieldValue = this.Host.InitParams[key];
    }
    return fieldValue;
}

This allows me to quickly add a lot of new parameters and write the OnStartup method as follows:

private void OnStartup(object sender, StartupEventArgs e)
{
    string DateTimeString = GetInitParam("eventDate");
    DateTime eventDate = DateTime.MinValue;
    if (DateTimeString.Length > 0)
    {
        try
        {
            eventDate = DateTime.Parse(DateTimeString);
        }
        catch { }
    }

    string backGroundUrl = GetInitParam("background");
    string startImageUrl = GetInitParam("startImageUrl");
    string endImageUrl = GetInitParam("endImageUrl");
    string registrationUrl = GetInitParam("registrationUrl");
    string registerText = GetInitParam("registerText");

    // Load the main control here
    this.RootVisual = new Page(
        eventDate,
        backGroundUrl,
        startImageUrl,
        endImageUrl,
        registrationUrl,
        registerText);
}

Notice that I’m calling a custom constructor on the Page class. That looks as follows:

Timer _timer = null;
public Page()
    : this(DateTime.Now, "", "", "", "", "")
{}
public Page(DateTime eventDate, string backGroundImageUrl, 
string imageStartUrl, string imageEndUrl,
string registrationUrl, string registrationText) { EventDateTime = eventDate; // Required to initialize variables InitializeComponent(); if (backGroundImageUrl.Length > 0) { backGroundImage.Source = new BitmapImage(new Uri(backGroundImageUrl, UriKind.Relative)); } if (imageStartUrl.Length > 0) { image.Source = new BitmapImage(new Uri(imageStartUrl, UriKind.Relative)); } if (imageEndUrl.Length > 0) { image1.Source = new BitmapImage(new Uri(imageEndUrl, UriKind.Relative)); } if (registrationUrl.Length > 0) { linkRegistrationButton.NavigateUri = new Uri("http://" + registrationUrl); } if (registrationText.Length > 0) { linkRegistrationButton.Content = registrationText; } _timer = new Timer(new TimerCallback(Timer_Tick), null, 0, 1000); AnimateLogos.AutoReverse = true; AnimateLogos.RepeatBehavior = new RepeatBehavior(1000); AnimateLogos.Begin(); }

That’s all that was required for making it parameterized so that I can update it quickly.