Implementing contracts in the AutoCAD MRU browser using WinRT
As part of my quest to understand WinRT more completely, I had a few goals for the MetroCAD application we saw in this previous post. Firstly, I wanted to add some contract support to the application, allowing it to participate in operations that Windows 8 has standardised across Metro-style applications. The main contracts I wanted to support were for search, sharing and settings. Secondly, I wanted to be able to launch AutoCAD – or another DWG editor – from within our Metro-style application.
The three contracts I wanted to support are all accessible via the “Charms” menu in Windows 8: if you place the mouse cursor at the bottom right of the screen – or swipe left with your finger from the right-hand side of a touch-sensitive screen, although I have trouble getting this to work – then your Charms should appear (see the image to the right).
What’s really nice about contracts is how well the application integrates into the Metro interface: settings are all found in one place, our app – along with many others – participates in unified search, and sharing – sending a link to a drawing by email, for instance – becomes really easy for the user.
Check out this video of the app’s new capabilities in action, before we look at some of the implementation details (albeit without diving into code):
Adding support for search inside the MetroCAD app was pretty straightforward: when the App is launched (i.e. from our OnLaunched() event handler), we register for the QuerySubmitted event for the current view’s “search pane”. In our handler for this event, we navigate to the search results, which we generate as a new “group”. If you’re interested in the code, look at the implementation of OnQuerySubmitted() and where it calls into.
Just as we hooked into the search pane, we need to add an event handler to the current view’s “data transfer manager” to implement sharing. We handle the DataRequested event – for the ItemDetailPage, as we want to enable sharing for individual items only – which populates properties of the requested data with information from the selected item. Check our the OnDataRequested() event handler for the details.
Once again, to implement settings we get the “settings pane” for the current view and attach a handler to the CommandsRequested event. From here we add some “commands” to set various settings – in our case some simple buttons to toggle sorting between “by day” and “by name”. These modify the ApplicationData.Current.RoamingSettings for our application, which – under the hood – is really cool: these settings are automagically synced via the cloud (assuming you’ve logged in using your Windows Live account, it’ll happen via that) and propagate across your various devices.
I had thought this would be much harder than it was (and in some ways it would be, if we wanted to launch a specific version of AutoCAD, for instance): while WinRT prevents Metro-style application from launching arbitrary “desktop” applications, it does let Metro-style applications open a file with one of its registered handlers. So as long as AutoCAD is installed on the system – and is therefore registered to work with DWG files – then we can request that a DWG file be opened, and AutoCAD will be one of the options presented to the user.
Well, it’s almost that easy. As the WinRT sandbox limits apps from accessing arbitrary files on the local system, we do have to register MetroCAD as being interested in reading DWG files in our application’s manifest, and we then have to limit loading files from “known locations” such as the “My Documents” folder. For the sake of this demo app, I just copied the various files into the local “My Documents” folder, so the app could find them there. For details on this, check the OnDoubleTapped() event handler, which shows how to use GetFileAsync() and then Launcher.LaunchFileAsync().
And before I forget, here’s the updated source project.