Replicating TargetNullValue in Windows8

There are times in this world when using binding statements, that you want to specify a default value for a property if the result provided is null. In a post found here by Jesse Liberty, he details how developers can utilize the TargetNullValue option within the xaml binding statement to provide this option.
Recently I’ve been porting my SaintsFC WP7 app over to Windows 8 metro app & I’ve found that this option is no longer available. It’s been confirmed that this will likely not be in the RTM version of Windows 8 either so here’s a workaround using a converter as a substitute. It’s a little more convoluted than a simple property within the binding but it will also be able to provide you with a more extensive feature set as a result.

So for wp7 my binding statement may have looked something like this:

<TextBlock Text={Binding Path=’Salutation’, TargetNullValue=’Hello Sir’} />

However for Windows 8 it looks a little something like this:

<TextBlock Text={Binding Path=’Salutation’, Converter={StaticResource TargetNullValueConverter}, ConverterParameter=’DefaultSalutation1’} />

Not quite at neat as the SL/WP7 implementation, but it does have a few benefits:
– You can change the paramaeter to provide a different results
– You can perform more complex actions.

How to implement:
Create a blank application, and add a folder called “Converters”.
In Converters folder, create a new Class called “TargetNullValueConverter.cs”
Then make the class public and implement the IValueConverter interface.
From here we’ll add the logic which will provide the basis of the TargetNullValue replacement. In the ‘Convert’ method enter the following code:

if (value != null)
   return value;
   string returnValue = String.Empty;
   switch ((string)parameter)
      case "SelectedImage":
         returnValue = "/Assets/SplashScreen.png";
         returnValue = "/Assets/SplashScreen.png";
return returnValue;

Firstly the code checks to see if theres a value, if there is, then theres no heavy lifting involved and the converter can just return the original value. However, if the value is null the bulk of the logic kicks in. I find that adding a Switch statement is the easiest way of handling the return value as it means you can write the converter just once and for every circumstance where you need a different value to be returned, you just write a new case.

To demonstrate this method I’ll talk through a quick demo. First, add a ImageItem class and implement INotifyPropertyChanged then add a property of type ImageSource named SelectedImage.

Next we’ll go to the MainPage.xaml and create our interface.
Add the following declaration


Set the data context to the ImageItem that you created a minute ago

   <local:ImageItem />

Then add the converter to the Page Resources

   <converters:TargetNullValueConverter x:Key="TargetNullValueConverter" />

Add the following markup to the main grid:

   <RowDefinition Height="Auto" />
   <RowDefinition Height="*" />
	   Content="Customize Picture"
	   Click="GetPicture" />
	   Content="Reset resource to null"
           Click="ResetPicture" />

    Source="{Binding SelectedImage, Converter={StaticResource TargetNullValueConverter}, ConverterParameter=SelectedImage}" />

That’s the Markup done, now go to the code behind.

In the constructor, cast the DataContext to a static instance of the ImageItem object

imageItem = this.DataContext as ImageItem;

Then create an asynchronous method called which utilises the FileOpenPicker class to get an image and save it to an object with a property of ImageSource then hook up GetPicture() to call it.

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
   var stream = await file.OpenAsync(FileAccessMode.Read);
   BitmapImage img = new BitmapImage();
   imageItem.SelectedImage = img;

For ResetPicture() just set the object’s ‘SelectedImage’ property to null;

imageItem.SelectedImage = null;

And that’s it, all that’s left is to build & run the code and you’ll be able to see this in action.

If you want to view the full source code you can download it here.

Windows Phone Marketplace content guidelines

In recent weeks there have been a few people who have raised concerns over certain content within the Marketplace, including certain spam applications and the misuse of keywords for non-relevant apps.

Todd Brix from Microsoft has written this quick post on some of the changes the Windows Phone Marketplace team will be implementing in the coming weeks to ensure a better experience for Windows Phone consumers.

The article details some important restrictions to keyword usage, as well as some guidance into tile design for some bulk app models.

Click the link below to read the full article.


Full Article: WP Developer Blog – Todd Brix

Extra properties when data binding

When you are binding data there are additional properties that allow you to format the display and to handle errors and null values. These are a valuable addition to any data bound control to ensure your user has a great experience.

The StringFormat property allows you to add any standard .NET format string that matches the type you are binding to, the FallbackValue instructs the binding on what to display if the binding fails and the TargetNullValue instructs the binding on what to display if the value bound to is null.

Full Article: Jesse Liberty

Panorama Backgrounds & Minimised AppBars

Before the Mango update, Windows Phone developers were advised against using the application bar with a Panorama control. This was somewhat changed with the Mango which includes a new minimized mode for the application bar. This mode can be accessed using the Mode-property of the application bar.

For example the built-in People hub uses this new mode. But if you cycle through the panorama items in the People hub, you’ll notice that there’s one panorama item where the application bar is shown in its default mode. This is a nice behavior because it draws the user’s attention to the functionalities provided by the application bar. And this same behavior has been replicated by many 3rd part applications. But be careful with it, because in its current state, changing between the two different modes may cut some pixels from your panorama’s background.

Mikael Koskinen has written a short piece on how to overcome this problem and ensure a continuous user experience.

Full Article:

Microsoft Advertising SDK for Windows Phone in the UK

The Microsoft Advertising SDK was only available in the US for the initial Windows Phone launch. With the release of Mango however, 11 additional countries were added including the UK.

What does this mean? It means it’s really simple to integrate advertising into your Windows Phone application (or game, Silverlight or XNA). Microsoft Advertising Network servers deliver ads to your users and you get a share of the advertising revenue.

In-App advertising can offer a great alternative mechanism for monetizing your apps. Instead of charging up-front, you offer the app for free and make money through advertising. Or you offer a free, ad-supported version alongside a paid, ad-free version. Experimenting with different models may itself prove to be a very worthwhile investment!

Mike Ormond has written a quick post on how to implement the Ad SDK into your Windows Phone Project.

Full Article: Mike Ormond’s Blog

Using full container width for ListBoxItem

A couple of days ago I was building a ListBox for a wp7 app which required the ListBoxItems to use the full width of the container. I tried to apply some of my WPF knowledge to fix the problem, using HorizontalAlignment=”Stretch” but to no avail. It took me ages to find a work around & I ended up using fixed width’s. For now, a fixed width solution is okay in portrait mode, but if I wanted the app to support both orientations, I would need a better solution.

This morning I stumbled across this post which explains that there is a bug in the coding for the WP7 Silverlight ListBox and the correct XAML code to fix it. Click the link below to read the full article.

Full Article: Timdam’s blog

Music Voice Command using WP7

I’ve already tweeted this a few days ago, but thought it needed a special mention. The other day The Verge picked up on the video from @KeyboardP. At the time the video was only posted as a proof of concept, but I have to say, that it’s awesome. Following the initial response from the video, KeyboardP has put together a quick FAQ which answers people’s immediate questions. It’s definitely worth a watch & I can’t wait for the first release.

Full Article: The Verge
Via: @KeyboardP

Using the system tray to show progress bar in wp7 mango

So here is a welcome discovery. After implementing the PerformanceProgressBar in my first NoDO app, I really thought that there needed to be a simpler way to implement a progress indicator…and now it’s here.

Ducas Francis has written up a quick piece on how to use the new built in progress bar that can be found within System Tray, along with an overview of the other additions new to Mango

Full Article:
Via: WindowsPhone Geek

How to stop toolkit transitions affecting performance

With the new Mango toolkit now fully released, there’s no excuse for poor performance. When building any new mango app using the new Performance Analysis is a must. This helps identify any threads in your app which are severely impact the runtime performance.

This article by Matt Lacey, explains how he identified a problem with the toolkit transitions, which can impact performance in a major way.

Full Article: