Introduction to Windows 8 Apps for Software Developers

by Dmitry Kirsanov 29. December 2012 16:24

Earlier this month, I wrote an introduction to Windows 8 Store for those who didn’t attend any course or online event, but would like to learn about it.

I met quite a few people so far, who wanted to go through Windows 8 development labs without knowing anything about how Windows 8 works, what are the new key components and so on. Needless to say, practice without even the basic theory is a waste of time.

So, this is the second part of about 10-part introduction to Windows 8 for Software Developers, and this time we’ll talk about the Metro Style application principles – what they are, how they work, what you should know first.

The difference between Windows 7 and Windows 8

I think you’ve already seen the new graphical user interface (UI) of Windows 8, so you already know how differently it looks from Windows 7 or Vista. However, that’s just the tip of the iceberg. The real difference of Windows 8 is how it works.

Here are the main difference points at glance:

  • New Windows Runtime (WinRT)
  • New User Experience (UX)
  • New Application Execution environment (with it’s own rules)
  • Windows Store

I spoke about the Windows Store previously, as I consider it to be a very important difference, but just the names of other three are not convincing enough, especially the “new user experience”, which sounds like an empty buzz word. So, what are they?

WinRT

Windows Runtime, or WinRT, can be explained as the new .NET framework, but different.

The first difference is that it’s not the framework that loads each time your application is executed, like we have with .NET. The problem of .NET is that every time you execute an application, system resources are spent of components that your application depends from. Not a big deal for powerful machines, but mobile phones and tablets have very limited resources – be it RAM or CPU, and if you can exclude that layer from equation, you’ll win in performance.

.NET framework is passive. It loads when your app starts, it follows your commands, it unloads when there are no .NET apps to execute. It doesn’t manage your application’s execution, even though it cleans up some resources and has some active elements like ASP.NET State management.

WinRT is active. It manages the life cycle of your app (see below), it is part of the system and doesn’t need to load specifically for your application, and what’s very important – it’s asynchronous, meaning that your applications are multi-threaded by default.

Speaking about asynchronous operations, it’s a giant leap big achievement and a well known head ache of the past.

With Pentium 4 processors in 2002 (and XEON processors a bit earlier that year), we’ve got the technology called Hyper-Threading(HT). It provided us with virtual processor cores – basically, you would have two pipelines of tasks for each physical processor, so if your application would consume two or more threads, they would execute simultaneously and finish earlier.

But not everything was cool about Hyper-Threading – for example, most of applications we made with .NET were single-threaded, especially web applications. And I’ve seen cases, when such web applications could perform about twice slower when HT was enabled, than when HT was switched off. So it’s a double-edged sword.

However, for a multi-threaded applications, HT was a bliss, so we tried to use multiple threads whenever possible. It was whatever you want, but not a simple task, especially when it comes to debugging, so when Microsoft was working on .NET 4.5, they made asynchronous execution a native part of the language and of the framework. You can just use the async and await keywords to execute functions asynchronously, and for .NET 4 (the last .NET version supported by Windows XP) they made an async library, which provides just the same functionality.

But .NET itself is not asynchronous by default. WinRT is. Since the majority of processors we are using now, including the ones in the mobile devices, are having multiple cores, asynchronous operations is a must. In some mobile scenarios this could even save the energy, as you could run more operations consuming a particular device within a shortest time frame possible.

rubiks-cubeWinRT also serves as a postman between different applications, allowing them to share data – that’s what Microsoft is calling winning as one. Imagine it as Rubik’s cube – where many separate components form a new entity.
Using other applications as the source of content, you can form truly unique ecosphere, where many different things form a masterpiece.

As you’ll learn from examples, WinRT looks and feels just like .NET, from developer’s perspective. Namespaces, classes, functions and data types – everything is similar. The main difference is that WinRT makes it simpler, is orchestrating the suite in which your app is soloist, and is enforcing best practices in resource management, making your life easier.

New User Experience

I won’t use words “better” or “worse” when describing the new Windows 8 user interface. The user experience (UX) is not only about Metro, and it’s not even about “natural behavior” and science of cognitive psychology that was involved in development of new user interface. What’s more important about the new user experience is that everything becomes predictable.

Microsoft developed quite broad guidelines to follow when developing “Metro” style applications (now called Windows Store apps) – some of them in form of proposal and some of them are enforced (read: your app will be rejected if requirement is not met).

Same elements, at the same places, same principles of placing the content, same behavior of applications (because now many things are managed by WinRT, not by you as developer) – applications look like they were developed by the same UX experts. This greatly simplifies development and at the same time – allows to cut the learning curve. If users know how to use other Windows 8 application, they know how to use yours.

Because of the mentioned winning as one, i.e. using contracts to get specific information from other applications, users can do more with Windows 8, than previously. For example – get information / photos / lists of contacts from user’s social networks accounts without even knowing that such social networks exist or that such social networks even allow connecting using API. Just because user has official applications that can provide you with that information.

Interactive tiles, cloud computing and management – all these features are native now and do not require any hardcore software development experience. In fact, usually it’s up to 5 lines of code.

We’ll cover it later.

Windows 8 Application Execution environment

Your application is an interactive content now. Very interactive, indeed, but it’s not independent anymore. When user expresses the wish to execute your application, it’s WinRT that loads your application – it’s code and resources, and it’s WinRT who makes decisions, your application only obeys.

There Can Be Only One

Only one active application is allowed to run and consume resources.
As soon as user switches to another application, your application get’s suspended. WinRT waits for 5 seconds and, unless user switches back, informs your application that it’s time to pack things and wait the end. Literally.

What your application must do then, is to save it’s state to disk, after that it stops getting any CPU cycles. Application becomes partially unloaded from memory, gets no CPU and so it doesn’t work, which also means it doesn’t drain the battery of mobile device.

How apps get suspended and terminated in Windows 8

When user decides to switch back to your application, the first thing you have to do is to load the saved state. Interesting, that you can load that state even when and if your app was terminated.

And speaking about termination – as you might guess, you get no warning about it, for two reasons: first, you’ve got no CPU cycles to listen to any event and react, and second – you’ve already saved the application state, so there is nothing else you could do. WinRT will terminate your application automatically.

Termination may occur, for example, when system experiences deficit of resources. Since your application still uses most of RAM it used when was active, unloading it can save the day. Other reasons may include switching users, system shutdown, application crash… WinRT will inform you about it when your app is executed next time, when it’s time to make decision of what to display to your user – the previous state of the app or starting from scratch.

What advantages we can get from that principle of having only one soloist in orchestra? Well, for example, if you are eager to count how much time your employees are working in your business application, you can instruct your program to ping your web service every minute or so. When user switches from your application, the app gets suspended, gets no CPU cycles, and so can’t check-in in your web service.

Another advantage, is that your application is granted most, if not all, screen “real estate”, so you shouldn’t worry about elements not seen in the screen, because user resizes your application to 400*400 pixels square.

And, of course, you get all resources of the device.

What are the disadvantages?

Obviously, you must entertain user, to keep his attention. In multi-window environment, it’s ok to have many applications working in background even if you have no immediate interest in them. In Metro – they will be suspended or at least restricted in functionality.

I would like to stress it out – you must entertain your user with your content.

Here we are now entertain us

You may be asking now – why should you care about the user’s attention span and “entertain” him more than previously? Here is the list:

  1. Your application is not working (and that’s the rule with exceptions, of course) when it’s not on the screen. And only one application can be on the a particular screen. Yes, some users may have more than one screen.
  2. When your application is forgotten for two weeks in Windows Phone 8 device, it loses it’s background task rights.
  3. Your application is only one click away from uninstall.
  4. Start menu is like billboard top – unless your application is pinned, it will sink in the list of other apps. So make sure your users love your app enough to pin it.

Starting the Application in Windows 8

Is very different from Windows 7. See, we have no shortcuts now, and tiles are not exactly the same thing as shortcuts. First of all – they can lead you to a particular page of the application. For example – the Finances application in Windows 8 has multiple sections – for stocks, currencies and whatever. So you can pin a particular page, say, with currency exchange rate for British pound, to your start menu, and every time you’ll click that tile you’ll go to GBP rate page.

Tiles are glass doors to your apps. They show you the latest updates (for example – mail application may show you the subject line of the latest e-mail) without showing the main page of the application. You can update them even from the cloud, without executing a line of code from within your application. As I said earlier – your app is a very interactive content now, a book that can be opened at any page and closed at any time.

Execution = Trigger + Condition

And that’s not the only new way to run your app. Or should I say – to run the code from your app. Here is another example – the Lock Screen.

Metro Lock ScreenYour application can display information on the lock screen and update it using the system triggers with certain conditions met. Like when internet becomes available and user present (so you could ask him for login and password to get connection to your network, for instance).

 

Metro App Execution Trigger Condition

And as I mentioned earlier, WinRT will inform you, when your application starts, what was the reason your app was terminated last time. Was that user, system shutdown or something else. But that’s not all the information you’ll get.

You also get the information about the states of various services and conditions,


Rules of a thumb

There are few. First of them – your users have no idea about suspension and termination. Which means – there shall be no sign that your application was terminated, if it was terminated from suspended state.

Metro App Activation RulesAs I said, you get that information during the activation of your app, so it’s up to you to make that decision. However, for your app to be predictable, it should behave the same way as other applications.

As you can understand, system is not going to wait forever for your user state to serialize to disk, so if there is a lot of stuff to save, you should do it incrementally, when things change. Then, when system will inform you that you should prepare for immediate suspension, you won’t have any problems.

Obviously, the user state should not be restored after the application was crashed, and it should not be restored if user used the tile to activate your application, as it usually means he wants to start from scratch.

Second rule – do not wait for data to be loaded. Load it asynchronously, and update the list when you get fresh information. Take, for example, the People application in Windows 8. Once you connect it to your social network account, it starts loading contacts from it, but you see the program’s user interface immediately, with new contacts beautifully appearing where they should be.

You never know the speed of the internet connection of your users, and you only have 15 seconds to show your main window, or your application will be terminated. I’ve seen applications that didn’t really follow this guideline, but it doesn’t mean that your application may get the same indulgence.

Third rule – do not “enhance” your user interface with “chrome”, i.e. elements that have no purpose. 3D buttons, shades, frames – forget about them altogether. Only add things that have a good reason to be added and bring a message with them. Use animations and glyphs that are part of WinRT, so your users will re-use their existing experience, instead of learning basics of user interface for your particular app.

That’s all for today, sorry for a long long text, but much more to come, I’m afraid :)

blog comments powered by Disqus

Month List