Wednesday, July 24, 2013

Fixing SelectedItem must always be set to a valid value

In the Windows Phone app I'm currently working on, I kept getting an "SelectedItem must always be set to a valid value" exception from a ListPicker on the OnSelectedItemChanged event.  It wasn't happening every time, but often enough that it was driving me crazy!

My collection consisted of fairly simple objects for the user to choose from, and I had a view model property for binding to the selected item.  The underlying issue is that it wasn't recognizing the selected item (in the binding) as a member of its list in ItemsSource property.  It turns out that it was a really simple fix.  By simply overriding the Equals method, my problems went away.  This enabled the control to see the selected item as equal to an item in the list.

If you've never done this before, you just need to add a method in the class to compare some key property, like this:

public override bool Equals(object obj)
    var other = obj as ThisClass;
    if( other == null ) return false;
    else return (this.SomeProperty == other.Property);

That's it!  Customize as needed, and you'll be good to go!

Wednesday, July 17, 2013

Glyphs for your apps

Instead of looking for images for all of your buttons, a much more lightweight way to add images in your apps is using font glyphs.  You've surely seen Wingdings and Webdings, but did you know there's also a Segoe UI Symbol font on your system?  You can use glyphs from this font in your Windows and Windows Phone apps.  All of the images follow the modern (Metro) look-and-feel so they'll look great.  The only problem is that you can't yet use glyphs in the application bar in WP.  Hopefully that will make it in a future update!  The link below will show you all of the available images so you don't have to preview them in Word.  To use them, just take the hex value and encode it using standard XML encoding:

<Button Content="&#xE206;" />

Miscellaneous icons
Small sample of glyphs from Segoe UI Symbol


Fixing "Multilingual App Toolkit build completed with errors"

I was having lots of problems with a project after adding Multilingual App Toolkit support.  It just stopped building and gave lots of build errors.  After talking to the MAT team folks at Build, I learned that reason for the issue was having resx files in the root of the project.  For some reason, it required them to be in a subfolder.  The fix is easy: either move your resources (like they really should be!), or...
...update to the newest version of the toolkit!  It turns out that the bug has been fixed.  All works great now.  If you were holding off on updating for any reason, do it!  It's a great product and it gets even better!