I have been doing a series of posts around my adventures in creating a cross-platform Dropbox library for Windows Phone and WinRT. I actually have a working API at this point, so of course the next step is to look into implementing a File Open Contract.
One of the first things you will notice is that you have to return set of IStorageFile back to the caller. So you have to essentially hand back a mechanism that the caller can use to open your file from the cloud (without the caller necessarily needing to know anything about that). At least this is my understanding at this point. I might have to pre-download my files (something I will be testing very soon).
Regardless, I thought it would be nice to go ahead and implement IStorageFile based on an URI. So I have created (and am letting you tinker with) this class. Here’s what happens with it. You call the constructor of my class with a URI that can be used to access the file (aka download the file), and my class builds up a read-only file that the file system thinks is a regular old file (BUT there are a bunch of things unimplemented, so you won’t be deleting files for instance). You can even control how the class acts when an unimplemented function is called (it can silently fail or generate an exception).
Let’s see some code:
Yep, that’s 2 lines of code to copy a file from a URI to a file. Pretty cool hunh?
Implementation details (stuff I learned)
Implementing this interface is a little difficult. Well, not majorly so, but there are some gotchas. First of all, I am used to working with Tasks, something that you will get very familiar/comfortable with when/if you get into the Async stuff. The interface requires that you return IAsyncActions and IAsyncOperation<T>s. If you look at Task you will see that it implements this at some level, but casting does not work. After digging a bit I discovered an extension (which is a part of System). Essentially you can call your functions that return Task and Task<T> and then call AsAsyncAction() or AsAsyncOperation() on the returned Task to get it cast to the appropriate type.
So, here’s what I did with this class. I implemented the interface and then call individual methods that return Task based equivalents and then use the AsAsync___ extension methods mentioned above. My actual methods are virtual and protected (which means you could derive a new class from them and override the missing functionality and get a something that is closer to fully functioning). I’ll actually be doing this with my Dropbox implementation so I can pass Dropbox-based files to the OS and things will act like these are actual files on the file system. Pretty cool, hunh?
By the way, none of this is done yet. If you want to help out, let me know.
Also, my description of building a File Open Contract may be incomplete. Like you I am learning this stuff. There might be a future post where I state that everything I knew is wrong. As far as the Open contract goes, at this point I am assuming that I will need to hand back the object and the caller will be able to open up the file. I am hoping that the caller will not need any kind of Internet access defined, but if that happens I will be rewriting these classes a little (so that this isn’t required.
So here’s the link to where the project lives: https://bitbucket.org/DevTheo/metrodropbox/overview
Here’s the link to where the file lives: https://bitbucket.org/DevTheo/metrodropbox/src/3617e9282c60/Common/IStorageFile
Oh yeah, the code from the last couple Metro Nuggets can be found here as well. (there have been some updates. For instance the Windows Phone WebAuthenticationBroker object has changed a little due to a bug I discovered).