© 2012 Autodesk Developing a simple Metro-style application for Windows 8 Kean Walmsley Software Architect, Autodesk
Mar 29, 2015
© 2012 Autodesk
Developing a simple Metro-style application for Windows 8Kean WalmsleySoftware Architect, Autodesk
© 2012 Autodesk
About the Presenter
Kean joined Autodesk in August 1995, and has since worked in a number of different positions and countries (UK, USA, India and Switzerland). He recently moved from the Autodesk Developer Team into the AutoCAD Engineering organisation, where he now works as a Software Architect.
Since July 2006 Kean has maintained a development-related blog at: blogs.autodesk.com/through-the-interface
Kean holds a Licence d’Informatique from l’Université de Paris-Sud and a Masters in Computer Science from the University of Kent at Canterbury.
© 2012 Autodesk
Class Summary
Covers the basic concepts around the WinRT sub-system of Windows 8, showing how to develop a simple, touch-enabled application that acts as a companion to AutoCAD.
We will look at the various contracts available to WinRT developers, and understand the limitations inherent to working inside the WinRT sandbox, including the need for asynchronous calls into many WinRT APIs.
The sample covered - while not working directly inside AutoCAD - will extract MRU information related to AutoCAD drawings in a format that can be used by our Metro-style browser application.
© 2012 Autodesk
Learning Objectives
At the end of this class, you will be able to: Extract MRU information from the Windows Registry, storing it in XML Understand the limitations imposed on applications by WinRT, including
the use of asynchronous calls Create a simple, Metro-style MRU browser application Implement contracts to enable search, sharing, settings and snapping
© 2012 Autodesk
Introducing Windows 8 and WinRT
© 2012 Autodesk
Windows 8
Latest generation of the Windows OS A critical release for Microsoft A bold departure with Windows Runtime, while focusing on legacy
Hybrid of touch-oriented and “Desktop” environments Desktop apps – such as AutoCAD – will work with modest investment More “modern” apps will work on ARM tablets such as Microsoft Surface
Easily adapted to Windows Phone 8
© 2012 Autodesk
Windows Runtime – The New Sandbox on the Block
Microsoft’s answer to the Android and iOS execution sandboxes Commonly abbreviated to WinRT
.NET was not considered the best technology for targeting mobile Technologically or politically driven?
A new layer on top of the Windows OS A set of core API services Can be used from various languages No longer required or possible to use P/Invoke
© 2012 Autodesk
WinRT – How it looks different
A whole new design language has been created Not only for Windows 8 Also found on Windows Phone, Zune, Xbox
Previously known as Metro, but has since been rebranded “Metro-style” apps became…
“Modern UI style” apps … and more recently…
“Windows Store” apps
A full-screen, touch-friendly user experience
© 2012 Autodesk
WinRT – How it looks different
Inspirations Bauhaus Swiss typography Motion Design
Design principles Pride in craftmanship Fast and fluid Authentically digital Do more with less Win as one
© 2012 Autodesk
WinRT – How it is different
The tools are familiar, but this is pretty different
Different object model for OS capabilities Asynchronous operation enforced for anything but “instant” responses Strict control over disk access Application interop only via contracts or file associations
© 2012 Autodesk
The WinRT Object Model
Let’s take a look… VS 2012 command-prompt, navigate to “c:\windows\system32\WinMetadata” ildasm.exe Windows.System.winmd
These definitions contain no code, only metadata Part of the core OS, likely to be written in native code
Useable from a variety of languages via “language projections” Available for Native (C/C++), HTML (JavaScript) & .NET (C#/VB) Use ildasm.exe /project to look at CLR projection
© 2012 Autodesk
The WinRT Object Model
But it is .NET or not? No, it isn’t, but it’s made to feel like it Underlying mechanism is actually COM WinRT and CLR type systems are distinct
Merged by metadata adapter
If coding in .NET, you target the .NETCore CLR profile Only exposes approved APIs You can try to circumvent them, but not if you want to be on the Windows Store
© 2012 Autodesk
Asynchronous operations are now the norm
Any call that might take over 50ms is now asynchronous-only Means that any “long running” call doesn’t block the UI thread Ensures a fluid UI experience (no more whitened windows)
No great coding overhead with async/await Typical code looks like this:
var file = await KnownFolders.DocumentsLibrary.GetFileAsync( “file.txt” );
© 2012 Autodesk
Restricted access to the file system
Applications can access files in a few locations Modify Package.appmanifest to request…
“Capabilities” to add/change/delete files in specific locations e.g. Documents Library, Removable Storage
“Declarations” associating the app with specific file-types Permissions get requested when installing the app
More freedom when handling a user-selected file Selection implies permissions have been granted for that file
© 2012 Autodesk
Application interop
App interop has been deliberately limited in WinRT Even if underpinnings are based on COM Imposes good behaviour on WinRT apps, but restrictive
File system and Registry isolation make it hard to share data
WinRT apps can interoperate via the Share contract Apps can be tied together at the user-level via the Share charm
e.g. a photo can be “shared” with an email client
WinRT apps can launch the desktop app associated with a file-type
© 2012 Autodesk
Additional points about WinRT apps
Really intended for lightweight, tablet-focused apps 32-bit only, implicit memory limitations Applications don’t even have a close button
Termination/resurrection is at the whim of the OS
The expectation is not that apps such as AutoCAD get ported And without a WinRT-based AutoCAD, there is no WinRT API for it
So what application possibilities are there? Especially with interop being so limited
© 2012 Autodesk
What can be done with AutoCAD?
Companion apps are an interesting possibility Windows Store apps that complement desktop apps such as AutoCAD
A fun example is to browse AutoCAD’s MRU file data Browsing data is a great use-case for WinRT
The WinRT app can launch AutoCAD with a file If AutoCAD is currently associated with the .DWG file-type If the file is located in an accessible location
© 2012 Autodesk
Implementing an AutoCAD MRU browser
© 2012 Autodesk
Browsing AutoCAD’s MRU data in WinRT
Cannot access MRU data directly from a WinRT app AutoCAD’s data is in the Registry and therefore not directly accessible
A specific export needs to be run from a desktop-hosted app Could be inside AutoCAD or otherwise Write some files that are then loadable from within WinRT
With appropriate application permissions, of course Ideally an automatic process
We’ll use a new feature in AutoCAD 2013, the Core Console…
© 2012 Autodesk
Extracting AutoCAD MRU data from the Registry
Let’s build a simple C# app that queries the Registry Writes an XML file with the MRU data Extracts thumbnails from each of the drawings Also copies the drawings to a local folder
Contrived example, but allows us to launch
The project references AcCoreMgd.dll App will be loadable in AutoCAD and the Core Console Populates a folder that’s accessible by the browser app
© 2012 Autodesk
Creating the Basic Browser
Start with the VS2012 Grid View template A very capable app for data browsing Gives a great deal of basic capabilities
From here we can add the various capabilities we need
The first step is to get the data into the browser Some work needed to manipulate the data source And then have the XAML bind to it
© 2012 Autodesk
Semantic Zoom
Pinch-zoom to see data at different levels of detail Very handy for providing a summary view of your data
Requires some work in XAML to implement SemanticZoom.ZoomedInView SemanticZoom.ZoomedOutView
If using keyboard and mouse Ctrl + mouse-wheel
© 2012 Autodesk
Contracts
Implement contracts to support Windows 8 charms Share Search Settings
All require using the static GetForCurrentView() method On a variety of different classes More on this over the coming slides
© 2012 Autodesk
Contracts – Share
Implemented by DataTransferManager.DataRequested event Add a handler when your item is created Populate the DataTarget with the data you want to provide
Warning: the Mail app is a little strange as a Share target If you add attachments it blanks the Title and Subject There’s a Share Target sample on MSDN that works more predictably
© 2012 Autodesk
Contracts – Search
Implemented by SearchPane.QuerySubmitted event Add handler when app is launched Generate a group of items that meet the search criteria Navigate to that group so they are displayed in the UI
Easy to implement using LINQ
var query = from item in Items where item.Content.Contains(queryText) select item;
© 2012 Autodesk
Contracts – Settings
Implemented by SettingsPane.CommandsRequested event Add handler when app is launched From here you register your “commands”
When your command’s event handler fires, update the settings Application.Current.RoamingSettings
Follows user between devices via Live ID Adjust the UI as appropriate to reflect changed setting
© 2012 Autodesk
Live Tile Notifications
Apps can provide notifications to the user via Start screen tiles
Tiles can be static or live Different formats to choose from
If wide, the user can also choose to make them square
Managed via Windows.UI.Notifications.TileUpdateManager Cycles through up to 5 live tiles
© 2012 Autodesk
Snapping
Two apps docked on the same screen Both WinRT apps or one can be desktop Snapped view up to 320 pixels wide Very handy for companion apps
Implemented via XAML (or HTML) Snapped visual style Contains a storyboard animating controls
© 2012 Autodesk
Launching
It’s possible to launch the application associated with a file-type
var file =await
KnownFolders.DocumentsLibrary.GetFileAsync(“A.dwg”);await Launcher.LaunchFileAsync(file);
The file needs to be in a known, application accessible folder Limits the ability to open files in arbitrary locations Why our MRU extractor makes local copies of DWGs to be browsed/opened
© 2012 Autodesk
Summary
Windows 8 is new and parts of it are very different Windows RT, the mobile-focused sub-system
Windows Store app development familiar but challenging Restrictions intended to improve trust and responsiveness Significant restrictions when looking at desktop app interop
Possible to implement interesting new capabilities for companion apps Semantic Zoom Contracts for Search, Share & Settings Live tile notifications, Snapping, Launching
© 2012 Autodesk
Autodesk, AutoCAD* [*if/when mentioned in the pertinent material, followed by an alphabetical list of all other trademarks mentioned in the material] are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document. © 2012 Autodesk, Inc. All rights reserved.