Choosing a photo from the Photos library in Windows 8
I’m working on a Windows 8/RT app, and one of the pieces of functionality I needed was the ability for users to select a photo from their library and upload it (more on the uploading). I wanted to use the built in Windows 8 photo picker (a specific type of file picker) built around the Modern style. My app is built with the MVVM pattern, using a few different helper libraries, so I had to jump through a couple hoops in order to do it, but it wasn’t too difficult. Here’s the gist, but there was one gotcha.
The gotcha was that this code is happening in a function of my viewmodel (invoked from a button command), so it’s not running on the UI thread. If you try to use the OpenFilePicker outside of the UI thread, it will fail. In order to solve this, I moved this code to its own function PickPhoto and invoked it on the UI thread:
This Dispatch Helper is definitely a big help! It’s included as a part of the MetroMVVM project which is based off the old MVVMToolkit project. Then what you get is the Modern file picker: