Category Archives: Development

Discussion on development

It’s //build time!

For those of you who haven’t managed to convince your management to let you go to //build (and this includes me), you can see what you’re missing on the official //build 2014 site. The recordings of the sessions will also be made available there.


Having had a look at the session listings, there is big (although maybe not unexpected) news for Windows Phone and Windows Store apps. As ever, you can find the details 24-48 hours after the live events in the videos and slide decks (or just keep an eye on your favourite Twitter client / blog).

Remembering files from outside the application repository

A common scenario for file access in Windows 8 is to allow the user to pick a file from their hard drive, not necessarily from within one of the system-allowable locations such as the local data folder, pictures or documents library, etc. This is, of course, easily achievable using the FileOpenPicker control, but what happens if you want to persist that selection across runs of your app? If you just store a reference to the file location, subsequent runs won’t be granted access to the file and that could bring your app logic crashing down.

The answer is fairly simple. Use the FutureAccessList object in the StorageApplicationPermissions namespace. This allows you to store a file (or folder) along with a token in order to be able to use the IStorageItem later on.

This is a simple example page that uses the FutureAccessList to allow the user to set an image source, which is persisted across runs of the app. For the sake of clarity, I have removed XML comments from the code.



So, what is happening here?

1) In OnNavigatedTo, the app will check to see if we have already set an image file. Let’s assume for now that we haven’t and skip forward.
2) When the user clicks the “Load Image” button, we show a filtered FileOpenPicker to pick a single file. Once we have the file back, we stash it in the FutureAccessList, along with a token to identify the file – in this case, just a string “pickedFileToken”.

3) I have moved the logic to render the file content to the “MyImageWithFileAccess” image out to a separate method. This opens a stream and sets the image source accordingly.
4) On the next run of the app (or visit to the page), we go back in to OnNavigatedTo. This method checks the FutureAccessList to see if we have set an image. In this case, we have, so it retrieves the StorageFile instance from the list and calls the SetImagePath method once more.


  • There is no error handling here – if the user deletes the file, we will get an exception.
  • We are not removing items from the FutureAccessList if we no longer need them. This can be done by calling the Remove method on the list, passing in the token, or clearing the list entirely with the Clear() method.
  • The FutureAccessList can hold up to around 1000 items, but try to use it sparingly. If you are having to keep track of 1000 items, you may need to re-think your app logic.

Discrete Slider in Windows Phone

This morning I found an interesting question on StackOverflow asking how to make the default Windows Phone slider control return discrete values (in this case an integer value rather than the default double).

There are several possible ways to go about this, but the one that leapt out was to create an attached property on the slider and bind to that.

The entire helper class for the property looks like this.

The points to note here are:

  1. On line 7 we hook into the slider’s ValueChanged event so that we can round the value.
  2. Ob line 12 I just return the slider’s Value property, cast as an integer. This value has already been rounded and set on line 7.
  3. If we don’t attach the property, the slider will continue to behave as normal

To use this code in an app, here’s a sample of three sliders, each with a TextBlock to display the Value.

I know this isn’t an elegant solution to the problem, and that it has a couple of issues, but it does work and should give the results the original questioner was after.

Syncfusion e-books

Syncfusion have released a series of e-books on various topics, including Windows Store Apps. You need to enter some registration information to be able to access them, but the content looks good at first glance – certainly good enough to make it worth registering.

Nokia Imaging SDK and the Emulator

If you’re testing an app that uses the Nokia Imaging SDK (or any other app that access your pictures library, for that matter) and you can’t see any content in your pictures library it might be the result of a bug in the emulator.

If you need to access the in-built images, you need to open the photos app at least once before you run your app. once you’ve done that, the content will become available to your app.

(Thanks to the guys at Nokia, this information has now been added into the documentation. I’m childishly pleased to have been able to help out in my own small way. ;) )

Nokia Imaging SDK

I’ve just been having a look at the Nokia Imaging SDK (also available via NuGet). It looks like it really bridges the gap that a lot of developers have when they want to include image technologies in their apps. (for example, you can specify an image size that you want to use in the PhotoChooserTask, but you can’t let the user decise what aspect ratio to use. It’s even less flexible in the CameraCaptureTask where the user can’t do any manipulation at all.

Adding this SDK would allow you to give a lot more control to your users as it supports cropping, resizing, rotation, scaling, filters and other image enhancements.

There’s a tutorial application project in the full downloadable version of the SDK (not available via NuGet) that will quickly get you started. Give it a try! (I would highly recommend you read the Quick Start guide as well, though. ;))

Unit Testing MVVM Light Messages

MVVM Light has a messenger implementation which can communicate with the UI layers in order to handle things like page navigation, and using this is is very easy to set up a PCL ViewModel assembly to drive both a Windows 8 and Windows Phone app. As a quick example of how you’d set this up, imagione this implementation in a view model.

where NavigationRequestMessage and NavigationTarget are implemented as follows.

Now you can handle the incoming message in your front-end app. In this example, it’s in a Windows 8 Store app.

Note that we unregister the handler again on navigating away from the page, otherwise the message will be handled twice next time we visit the page, three times the next time, etc.)

Now we can write a unit test for this method to ensure that whatever our actual implementation logic tell us should be the resulting navigation is actually correct…

This is a trivial example, but hopefully it explains how to test the navigation in your apps when driven by MVVM Light’s Messenger functionality.

Another Unit Testing trick

So, you’re about to start an MVVM Windows Phone app, and you really want to be able to run unit tests against your view models using Visual Studio’s built-in test engine.

By default, you can’t do that for a phone app – you need to create a Phone Unit Test App that will wrap everything and spit out the test results when you run the app.

If you really want to be able to run tests within the IDE you can build your view models in a separate Portable Class Library (yes, those again). As long as you add .NET as a target framework for the VM library, you will be able to write your tests in the same way as you would for any other project type.

Of course, this approach is not appropriate for all functionality – for phone-specific functionality you will need to use the default (separate app) approach, or a mix of the two.

Unit testing gotcha

In the course of writing unit tests on a project, I came across in interesting wrinkle for testing MVVMLight ViewModel classes. When creating a ViewModel, the work is actually done on different thread which leads to the following unit test code throwing an exception.

Instantiating the SummaryViewModel class leads to the following exception message being displayed.

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

The solution is relatively simple, though. First I create a method to allow me to run an action and marshal its result back to the appropriate thread.

And then I created a method specifically to create the SummaryViewModel, since I will be doing that many times across my unit tests.

Then it is just a matter of changing my test method to call CreateViewModel and await the result, and all is good.

Offline Nuget access – sort of

Nuget is one of my favourite tools within the Visual Studio ecosystem, but sometimes you need access to packages you have installed before while you are not connected to the internet. This simple trick shows you haw you can do just that.

NuGet caches all the packages it downloads in a subfolder of your AppData filestore. You can easily set this folder up to be a package source like so…

  • Open Visual Studio, and go to Tools -> Options
  • Find the “Package Manager” section, and expand it
  • Click on “Package Sources”
  • Click the “Add” (+) button in the top right
  • Set a name for your local cache (I call mine “Cache” – imaginative, huh?)
  • Enter the path “C:\Users\<username>\AppData\Local\NuGet\Cache” in the Source textbox
  • Press “OK”

Now when you open the package manager, you will see the Cache folder listed as a package source and you can install packages from there. Be aware, though, that packages may be out of date with respect to the online version, and dependency packages may not be available.