Category Archives: .NET

WP7 Navigation Gotcha

This afternoon I discovered a bug in one of the apps I am working on. It happens because the keys we use for navigation are provided in a third-party feed, and one of those keys contains a plus sign (“+”).

This wasn’t immediately apparent as an issue, and we just navigated using

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + myKeyValue, UriKind.Relative));

Unfortunately, this gets UrlDecoded on the receiving side as a sapce, so our key that started out being “text+text” was being resolved as “text text”. When looking that value up in our tables the key was obviosuly not found and a NullReferenceException was thrown.

The fix was pretty simple, though. We just had to UrlEncode the strong before we sent it, as follows.

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + System.Net.HttpUtility.UrlEncode(myKeyValue), UriKind.Relative));

and all is good with the world again.

Silverlight Toolkit ExpanderView – Flat Objects

Before anyone posts, I know this is not the intended usage of the control. I did, however, have a requirement to show a list of simple objects and some of their properties in an expander view, so I’m going to explain one of many possible ways to do it.

My ViewModel contains an ObservableCollection<ServiceAddOn>, where my ServiceAddOn class looks like this.

public class ServiceAddOn : INotifyPropertyChanged
    string name;
    string cost;
    DateTime startDate;
    DateTime expiryDate;
    // encapsulation for public access to the 4 locals
    // INotifyPropertyChanged implementation

Don’t worry about the encapsulation or INotifyPropertyChanged implementation – they are as simple as it gets.

Now, I want to create an ListBox of ExpanderView controls that will display the information like this :

My Item Name
- Cost : £123.99
- Start Date : 1/1/2012
- Expiry Date : 1/1/2013
Second Item

That is where the problems start. As far as I can tell (with admittedly limited research into the issue) there is no way to bind to properties of an object in both the HeaderTemplate and the ItemTemplate – something needed to be done to expose these properties as a collection. Since I was not able to change the inplementation of the ServiceAddOn class, I decided the best way would be to write a converter. Before starting that, though, I needed a class to hold each of myt exposed properties. It looks like this :

public class ExposedProperty
    public string Key { get; set; }
    public string Value { get; set; }

Again, it is about as simple as it gets. :)

Now, the converter:

public class ExposePropertyConverter : IValueConverter

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        ServiceAddOn obj = value as ServiceAddOn;
        if (null == obj) return null;
        ObservableCollection<ExposedProperty> values = new ObservableCollection<ExposedProperty>();
        values.Add(new ExposedProperty { Key = "Cost", Value = obj.Cost });
        values.Add(new ExposedProperty { Key = "Start Date", Value = obj.StartDate.Date.ToShortDateString() });
        values.Add(new ExposedProperty { Key = "Expiry Date", Value = obj.ExpiryDate.Date.ToShortDateString() });

        return values;

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        throw new NotImplementedException();

So now, it is just a case of a bit of plumbing…

Add the namespace reference to the XAML, and declare a converter instance

        <conv:ExposePropertyConverter x:Key="GetMyProperties" />

and then wire up the Listbox / ExpanderView combo… (note tht my collection of ServiceAddOn objects is called “DisplayItems”)

<ListBox ItemsSource="{Binding DisplayItems}">
    ...snip standard plumbing...
                 ItemsSource="{Binding Converter={StaticResource GetMyProperties}}"
                         <TextBlock Text="{Binding Name}" />
                             <Run Text="{Binding Key}" />
                             <Run Text=" : " />
                             <Run Text="{Binding Value}" />

The “magic” happens on line 6, where I bind the ItemsSource of the ExpanderView control to “{Binding Converter={StaticResource GetMyProperties}}”.

That’s it.

Next steps for this may be to make the Converter more generic, and possibly use reflection to get the properties instead of hard coding them, but for now this is good enough.

If you have any comments about a much simpler way to bind to flat, single-level objects in an ExpanderView control. I’d love to hear them.

Alter a Pivot controls header template the easy way (WP7)

If you want to alter the template for a Windows Phone 7 pivot control’s header, the simplest way is as follows…

        <controls:Pivot Title="Altered Styles">
                <!-- This changes to look of the items headers -->
                    <TextBlock Text="{Binding}" Foreground="Black"/>
                <!-- This changes to look of the pivot overall title -->
                    <TextBlock Text="{Binding}" Foreground="Black"/>
            <controls:PivotItem Header="daily">
            <controls:PivotItem Header="hourly">

Of course, all this example does is specifically set the colour used for the title and headers to black, but you can do (almost) whatever you like in those templates. Want to add an image as a bullet? Go for it.

        <StackPanel Orientation="Horizontal">
            <Image Height="48" Width="48" Source="/MyBulletImage.png" />
            <TextBlock Text="{Binding}" Foreground="Black"/>

Alternating ListBox item background colours in WP7

I had a requirement today to implement alternating row colours in a Windows Phone 7 ListBox. After a bit of frustration with searching and only finding answers tht said “You can’t do it”, or that you need to add a property on the model to bind the background to, I eventually hit upon a nugget of common sense on this page.

Basically you need to create a converter class that will handle the alternation of backgrounds for you. It is ridiculously simple once you see it in action.

Step 1 : Create the converter.

public class AlternateRowColour : IValueConverter
bool isAlternate;
SolidColorBrush even = new SolidColorBrush(Colors.Transparent); // Set these two brushes to your alternating background colours.
SolidColorBrush odd = new SolidColorBrush(Color.FromArgb(255, 241, 241, 241));

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
isAlternate = !isAlternate;
return isAlternate ? even : odd ;

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();

Step 2 : Add the converter to the page

		<conv:AlternateRowColour x:Key="RowColour" />

Step 3 : Bind to the ListBox

<ListBox ItemsSource="{Binding}">
      <Grid Background="{Binding Converter={StaticResource RowColour}}">
        <!-- layout XAML -->

And you’re done.

Fuzzy-Matching of names

Yesterday at work, I had to try to knock together a quick application to parse a database table and pull out records with a matching name.  Simple at first glance, but more complex when you think about the common mis-spellings and abbreviations of names.  For example, Jonathan Miller may quite rightly be represented as Jon Miller, John Miller, Jo Miller and so on.  What I needed was a way to disambiguate these names for matchin purposes.

Conventional wisdom here would suggest the use of soundex patterns, and I agree that there are compelling arguments for this approach.  However, being the contrary soul that I am I decided soundex wasn’t quite good enough and went looking for alternatives.

I came across this blog post outlining a SourceForge project from the Web Intelligence Group at the University of Sheffield.  I took 20 minutes to implement the pre-requisites to use the patterns, and have to say it seems to give me what I want.

I’m using the Jaro Winkler metric to provide the fuzzy matching I’m looking for, and I am also able to give the users a choice of the confidence level of the match.  A confidence level of 1 will only return data that matches exactly. Confidence level 0 would return everything.  A bit of trial andd error showed me that a confidence level of around 0.85 produced the best result for my purposes.  

An example of the query I used would be something like this…

PROCEDURE [dbo].[GetResultsByFuzzyName]
	@FamilyName varchar(35),
	@GivenName varchar(35),
	@DateOfBirth datetime,
	@certaintyLevel int

  DECLARE @cert float;
  SET @cert = CAST(@certaintyLevel as float) /10 
    ,dbo.JaroWinkler(upper(familyname),upper(@FamilyName)) as FamilyNameScore
    ,dbo.JaroWinkler(upper(Givenname),upper(@GivenName)) as GivenNameScore
    dbo.JaroWinkler(upper(familyname),upper(@FamilyName)) >= @cert
    dbo.JaroWinkler(upper(givenname),uppeR(@GivenName))  >= @cert
  order by 
    dbo.JaroWinkler(upper(familyname),upper(@FamilyName)) desc,
    dbo.JaroWinkler(upper(givenname),upper(@GivenName)) desc

I could go further with this and do a bit more analysis with a second (or even third) matching algorithm, but for now I’m getting pretty good results.

ASP.NET MVC – Deploying on Win XP

This is the second in a series of posts about things I found out while learning ASP.NET MVC. Today : Setting up your development XP machine to run your site.

This is actually quite a simple one, but be warned, the overhead of setting up IIS5 to run an MVC site may lead to trouble if you put it under heavy load.

First, you’ll have to add a new Route to your Global.asax.cs file, just for IIS5.  For example :


      "IIS5",  // Route name
      "{controller}.mvc/{action}/{id}",  // URL with parameters</strong>
      new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

Note in this example that the only difference (apart from the name) is the addition of a “.mvc” extension after the controller declaration.

Now, you just need to configure your IIS virtual directory.

In inetmgr, navigate to your virtual directory, right click it and select Properties.  On the default tab (“Directory”), click the Configuration button.  You now need to add a new file extension handler, so click on the “Add” button.

In the textbox for the executable, enter the path to aspnet_isapi.dll ([probably C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll), and set the extension to be “.*”.  Before you hit “OK”, make sure you untick the box marked “Check that file exists”.  Now you can hit OK a few times and you’re good to go.

ASP.NET MVC – Preparing for the real world

This is the first in a series of posts about things I found out while learning ASP.NET MVC. Today : adding jQuery support that will work wherever the app is deployed in a directory structure.

In order to reference the jQuery javascript files, I had to figure out how to add a realtive link to the .js files that would work even if the app was deployed to an unknown virtual directory. A quick question on led me to the following steps.

1) Create an extension method in the Helpers namespace…

public static string GetBasePath(this HtmlHelper helper)


    var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

    return urlHelper.Content("~/");


Next, point the link tags for my js file to the new location…

<script src="<%= Html.GetBasePath() %>Scripts/jquery-1.3.2.js" type="text/javascript"></script>

That’s it.  All I had to do.

Of course, since I want to call data back from my MVC site via jQuery, I had to take a couple of extra steps to make the base path available to my scripts.  Back in my master page, I created the following snippet.

<script language="javascript">

  var rootUrl = "<%= Html.GetBasePath() %>"; 


Now I can call the data back from the site within my custom-defined jQuery script as follows…

$('#preview').click(function() {

    $.get(rootUrl + "jquery/getprovider/" + $(this).attr("name"), function(data) { $('#detailDiv').slideDown(300); $('#detailDiv').html(data); });


My #preview object has an id coded into its ‘name’ attribute so the jQuery controller can reference the correct data., eg.

<a href="#" name="12345">details</a>

System.Linq – Unleash the power!

I’ve been poking about with the System.Linq namespace in C# 3.5 this week.  Like most “new” areas of a language, it can be difficult to find the time or motivation to delve into the capabilities it provides, but I’ve just had it proven to me that I should have taken the plunge months ago.

In one small project, I have to compare two generic lists and pull out the records that are equal across the sets, as well as those that are different from one set to another (additions and deletions).  Luckily, the sets are fairly small, so the implementation I wrote last week is good enough to get the job done.  This week, however, I decided to do a quick benchmark to see how much faster it would be to use the new Linq-y methods.

First up, I created a small test class – MyType.

public class MyType
	public int MyId { get; set; }
	public string GivenName { get; set; }
	public string FamilyName { get; set; }
	public DateTime DateOfBirth { get; set; }

	public override bool Equals(object obj)
		if (null == obj) return false;
		MyType compareTo = obj as MyType;
		if (null == compareTo) return false;
		return (FamilyName == compareTo.FamilyName && GivenName == compareTo.GivenName  && DateOfBirth == compareTo.DateOfBirth);

Next up, I created a comparer class, inheriting from IEqualityComparer<MyType>

public class MyTypeComparer : IEqualityComparer
	public bool Equals(MyType x, MyType y)
		return (x.FamilyName == y.FamilyName  && x.GivenName == y.GivenName  && x.DateOfBirth == y.DateOfBirth);

	public int GetHashCode(MyType obj)
		return obj.GivenName.GetHashCode() ^ obj.FamilyName.GetHashCode() ^ obj.DateOfBirth.GetHashCode();

Next we create two collections of 10,000 members each.  I did that using the following snippet.

private void SetupDataForTest(int numberOfRecords)
	recordSetOne = new List();
	recordSetTwo = new List();
	matches = new List();
	matches2 = new List();

	for (int i = 0; i < numberOfRecords; i++)
		recordSetOne.Add(new MyType {MyId = i, GivenName="Trevor", FamilyName="Test" + i.ToString(), DateOfBirth = new DateTime(1970,1,1).AddDays(i)});

		if(i % 3 == 0)
			recordSetTwo.Add(new MyType { MyId = i, GivenName = "Trevor", FamilyName = "Test" + i.ToString(), DateOfBirth = new DateTime(1970, 1, 1).AddDays(i) } );
			recordSetTwo.Add(new MyType { MyId = i, GivenName="Ian", FamilyName="Wilson" + (i*2).ToString(), DateOfBirth = new DateTime(1971,1,1).AddDays(i*2) } );

So now we can get to the meat of the thing and find all the records that exist in both sets.  The old way of doing this was looping through the first set, and for each member loop through the second until we find a match (I’m not going to provide code – it’s too darned ugly).  Doing it this way took around 8 seconds on my dev machine.

The Linqy way, though is to use the Enumerable.Intersect method :

private void RunLinqTest()
	var intersection = recordSetOne.Intersect(recordSetTwo, new MyTypeComparer());

Guess what?  Adding all the matches to a pre-existing collection returned the expected 3334 results, and took 7 milliseconds.Yes, milliseconds.  That means I could run this method 1142 times in the time it took to run the old way.

I’m not going to go into the specifics of how to pull out the added/deleted records (but I’ll give you a clue -> Enumerable.Except), but the performance gains were enormous there, too.

Windows 7 on the MacBook

I’ve just installed Windows 7 on my MacBook’s BootCamp partition, and so far it looks great.  I’m not new to Win7, having had it installed in a Parallels VM since it was released.  The experience on the MacBook’s native hardware, though, is so much better.  For a start, it doesn’t die when you try to run (or create) a WPF application.  For a developer, that’s a pretty important thing. :)

One niggle, though, I can’t get audio.  I know there’s a fix, but after inserting my Leopard DVD, the drive has stopped responding.  Now I have to boot back into OS X in order to eject the disc.

Oh well, I’m going back to playing with this OS, and seeing what it can really do.