Skip to content

A Better Safe String

May 4, 2012

public static string ToSafeString(this object obj)


return (obj ?? string.Empty).ToString();



Wardacku, A Fun Silverlight Time Wasting Game

April 22, 2010

So, I actually wrote this a long way back when Silverlight 3 was still fresh faced but never got around to posting it.  With Windows Phone supporting Silverlight, I thought I’d get it online and maybe some feedback before I start the refactoring to make it compatible with the upcoming phones.  I’ve posted a bit of code from the game before but might put up the whole source or tidbits if there is any interest.  Here’s a screenshot of the game.

You can play here: or hit the link on the side bar.

Helping TTS Pronounce Words

March 10, 2010

Again, for the app I’m doing a bit of TTS and part of it is reading the weather.  I noticed that a lot of voices, Microsoft Anna for one pronounces something like “25 mph winds” as “Twenty five miles per hour whine-ds” as in “I need to wind my watch or wind up that cord.”  But you can put your text into an SSML format and have your app SpeakSSML(string) so you can give it clues on how to read certain bits.  You can build paragraphs and sentences, speed up or slow down the reading, or fix pronunciation.  Heres a quick sample.

string GenerateWeatherSSML(string words)


string windsFix = “<phoneme alphabet=\”x-microsoft-ups\” ph=\”W IH N D S\”>winds</phoneme>”;

StringBuilder finalBuilder = new StringBuilder(“<p><prosody rate=\”-45%\”>”);

finalBuilder.Append(” <s>” + words.Trim().ToLower().Replace(“winds”, windsFix) + “</s> “);

finalBuilder.Append(“</prosody></p> “);

return finalBuilder.ToString();


You need to know the MS phoneme alphabet but it’s pretty easy once you do.

Simple LINQ Example

March 10, 2010

I’m working on a rather large WPF app and there are bunch of little bits and bobs that I’ve noticed might be of interest or at least a nice reference for myself.  Here is one where I’m updating a user’s profile.  During the process I’m creating a bit that schedules some tasks for users.  I have a static method that updates the users settings but I needed a way to make sure that I was getting rid of days they’d unchecked in addition to adding the new ones.  Heres how I checked to see what days they may have removed.

if(oldProfile != null) if 


List<DaysOfTheWeek> removedDays = oldProfile.DaysToWake.Keys.Except(profile.DaysToWake.Keys).ToList<DaysOfTheWeek>();

if (removedDays.Count > 0)


foreach (DaysOfTheWeek removedDay in removedDays)


TaskScheduleHelper.DeleteTask(profile.ProfileName, removedDay);




(oldProfile != null)


List<DaysOfTheWeek> removedDays = oldProfile.DaysToWake.Keys.Except(profile.DaysToWake.Keys).ToList<DaysOfTheWeek>();

if (removedDays.Count > 0)


foreach (DaysOfTheWeek removedDay in removedDays)


TaskScheduleHelper.DeleteTask(profile.ProfileName, removedDay);




Bing Maps Silverlight Control With Traffic Cams

September 10, 2009

So there is a CTP of the Bing Maps Silverlight SDK out there and with the discontinuation of the version it looks like this is the direction that Redmond is moving.   I’ve been playing with the controls for a few weeks now and thought I’d do a little tutorial on a practical use.  One of the many awesome things about using Silverlight for this is that it don’t bog down with lots of pins / objects on the map and we don’t have to deal with a lot of the AJAX and postback issues we would doing the same thing in a traditional app.

So despite the bad things that people in PA have to say about PennDot, they do have a ton of public traffic cams up on the major roads so an enterprising user can check conditions if they are going anywhere around the city.

In this app we’re going to generate a map and pins for each cam which will pop their image into a modal child window for the user to view and refresh the image every 5 seconds



That done, the first thing we need to do is add a ref to the map assembly.


You can do it like this.


Then in the xaml on the main page we need to add the reference thus:


We also add the object to the grid and give it a name.  If you run the app now you should get a normal map.  For this we’re going to need a Child Window to show the image in, so lets add one to the silverlight project.



Now that it’s added, lets go mess with the xaml a bit.


It has it pretty well setup for us to begin with but we want to bump up the size a bit to make the images more visible and add an Image object to the grid.  We don’t need to set its Grid.Row property explicitly since it’s the only one there and will fall into place on its own.  Give it a name and jump to the code view.

I’m giving you all the code at once so lets just step through and see whats going on here.  First we’re taking a string in the constructor which we’ll see come in handy later and throwing it into a class variable.  We’re also setting up a DispatcherTimer which is almost exactly like an old school timer.  We preload the image and start the timer ticking every 5 seconds and instruct the callback to reload the pic each time.


We can also see that loading the pic is super easy.  Its important however to make sure we’re not caching the image or it’ll never change.  In most cases this wouldn’t be an issue as Silverlight does this to save resources, in this case we need the opposite.

Now lets jump back to the main window.


I grabbed the data off the pennDot website and put it into a nice little xml file.   It looks like this:


With that added we need to start creating pins for the user to click to see the camera footage.


Now lets do this with LINQ to XML, we’ll need to import the assembly because it doesn’t preload with Silverlight.


That done we can write a little query to grab the data we need.


Now we’ve got an anon collection we can loop through and add our pins.  We’ll want to put them all into a new MapLayer to make it easy to clear them all later if we decide to give the user the option to remove the pins and it’ll simplify placement.


For this example I’m just making little green Ellipses for pins but you could do something nicer with ease.  You can see that the URL to the different images is predictable if we know the camera ID so we set that up and create an anon method that will run when the user clicks on an Ellipse.  The event will create an instance of our child window and point it at the camera URL the user just clicked on.  When the user is done it will release the mouse.

Now we add the ellipse to the new MapLayer we created and place it in the correct location.  The loop complete we add the new layer to the map.   One last change.  We’ll do the user a favor and center the map near our cameras and zoom it in so that they’re pretty well visible.


Now lets see how we did.


And the camera


You can check out a live version here

Html 4.1 Makes CSS Names Case Sensitve

September 7, 2009

We recently had to change a bunch of pages to make sure that IE and Firefox rendered them in standards mode to support some Ajax controls. It’s simple enough to add the doctype and an x-ua-compatible line for ie. What shocked me was when it broke our image buttons. They were linkbuttons with totally valid css behind them. The css validated from css 1 to 2.1 and I was baffled until I noticed the button was rendering correctly on one page where by rights it shouldn’t.

I fired up developer tools in ie8 and firebug and saw that on the bad pages the proper class wasn’t getting picked up by the buttons at all. WTF!?!

Then I saw it. The name of the class on the good page was case sensitive while all the others were capitalized. Could it be so simple? Yea, yeah it is.

tl;dr If you’re doing a page in HTML 4.1 standards, make sure you css classes observe case sensitivity.

Images and Parameters Broken in CrystalReportViewer

May 28, 2009


I hate Crystal Reports.  I was on the verge of a nervous breakdown today after launching a well tested app and finding my reportviewer with broken images or with a mysterious javascript error saying Object Expected.


The deal is that Crystal requires the aspnet_client folder that IIS creates in your Default Web Site and is accessable to all the virtual folders that live there.

However if you try to deploy your site to a new website on the server to use a different port or subdomain using host headers you won’t have access to the aspnet_client goodness and your reports will be borked.


You can either copy the folder into your application folder or create a virtual folder that points to the aspnet_client folder in the Default site.


and you’re golden.


I owe a co-worker with a good memory for putting me on the right scent to figure this out.  Hope this helps someone avoid the anguish this caused me.