Tip: Using Code Snippets in Visual Studio – Boredom Challenge Day 20


Code Snippets feature of Visual Studio allows us to save pieces of code in snippet files, and then easily put them to our projects with the press of a few shortcut keys. This is useful if there is a specific code that you have to use again and again in different projects (for example, the code that handles Facebook login), so instead of writing it each and everytime (or opening another project and then pasting from there) we can just use the code snippet. This is also useful if you are giving presentations about a technical topic since you can prepare the code you’ll use beforehand and don’t bother to write them from scratch during the presentation.

If you have Visual Studio open now, try it yourself: Press Ctrl+K, X; then select a snippet and press enter. The related code will be automatically added to that line.


Under “Documents\Visual Studio 2012\Code Snippets” folder, you’ll see that there are folders for different programming languages, each with a My Code Snippets subfolder where you can put your custom code snippets. If you also wish, you can go to Tools -> Code Snippet Manager in Visual Studio and add other code snippet folders.



➤ Read on if you wish to learn how we can make our own code snippets


Making Simple Windows Store Games in XAML – Boredom Challenge Day 19


If you wish to develop games for Windows 8, you’ll need to learn how to use Direct3D, HTML5 or a game engine that supports Windows 8 (like Unity or Construct2). Since XNA is not supported in Windows Store apps (and also since Microsoft has announced XNA will be discontinued), if you were using only XNA (like me) and you didn’t have enough time to learn the technologies I’ve listed, you may end up wondering how to create a game without you requiring to learn something entirely new.

If that is the case, you’ve found your easy solution. As long as the game is simple (don’t expect to put a physics engine or 3d models in there), we can use XAML’s features (like animations) and add the game logic to the codebehind files for a simple game.

Don't expect something like Angry Birds, but I bet we could make a side-scrolling platformer.

It may not be something like Angry Birds, but I bet we could make something enjoyable.

In this article, we’ll make an example game called… Catch the Cage!, starring Nicolas Cage. 😛 It will be a classic game where you tap the ball on the screen to keep it from falling out of the screen. Here’s a screenshot to whet your appetite. 🙂


➤ After this masterpiece, you won’t even bother to look at Angry Birds

Using Microsoft Advertising SDK in Windows Store Apps – Boredom Challenge Day 18


The main reason for developing software is, of course, earning money. 🙂 For desktop applications, the most common way of monetization is directly purchasing a license – the user pays a specific sum and gets to use the software. In the case of mobile development however, apps need to be very addictive or very useful, and overall need a lot of effort put into them to be successfully sold directly for a price. Another aspect of mobile apps is that paying for applications is usually a new (and alien) concept for many mobile users, and you can guess that they don’t like it much (I’ve seen many cases where a user gives bad ratings to a quite good game priced at a few dollars, just because it wasn’t free).

Click the image and view it in full size. You'll see that nearly all of the top "paid" apps are games, and they require a lot of effort to develop.

Click the image and view it in full size. You’ll see that “paid” apps require a lot of work to be successful (most of them are games, too).

This situation has resulted in alternative monetization techniques for mobile apps. One of them was adding trial versions which gave the users a taste of the app, and told them to buy it if they liked it. Another was to make the app free but to add in-app purchases such as removing limits or adding new items, characters or levels. And the final one was to put ads to gain revenue, either by making the app free and using ads as the main funding source, or making two versions of the app (a free version with (usually annoying) ads and a paid version without them).

In this article, we’ll see how we can go with using ads, and we’ll create an example Windows Store app that uses them. For this purpose, we have Microsoft Advertising SDK (for Windows Phone and Windows 8), that makes it very easy for us to include ads in our apps (with nice features such as using location to show more relevant ads). Now, I haven’t seen anyone who likes ads, but they are actually quite effective if used correctly, especially if your app has a high usage value (that is, if users would not just open your app once and don’t touch it ever again). This way, you can actually have a slow and steady income.

➤ This article has been sponsored by Nescafe and Visual Studio (no, not really)

Storing User Credentials Securely in Windows Store Apps – Boredom Challenge Day 17


It is quite common to need to store our users’ usernames and passwords (or other critical information, such as access tokens) in our apps, so our users won’t have to sign in each time they use the app. However, although I’ve said this is commonly needed, we need to be careful when storing them because we can’t afford even a slightest chance of them being exposed.

So we're talking about "Remember me" or "Keep me signed in" functionality.

So we’re talking about “Remember me” or “Keep me signed in” functionality.

In Windows Store apps, there is a small but potentially deadly pitfall when implementing this feature: Using application local settings for storing username and password. Application settings may be kept isolated from other apps and users but that isn’t enough. They are kept in the registry, and if you dig deep enough and know what you are doing, you may find a way to read them (check this link for starters). For example, in my previous blog posts where we integrated Facebook, we kept the user’s access token this way (for simplicity, of course), and if anyone were to read the application settings and find that token they could read our user’s timeline and/or post stuff on his/her behalf.

This is what will happen when your Facebook access token is exposed, at best.

This is what will happen when your Facebook access token is exposed, at best.

Anyway, joking aside, the correct way to store critical information in Windows Store Apps is to use credential lockers; namely, the PasswordVault and PasswordCredential classes under Windows.Security.Credentials workspace, and in this article we’ll make an example app that keeps the user’s username and password securely.

➤ I have a feeling that “gay” joke is going to give me some headaches when my friends see it

Http multipart/form-data Upload in Windows Store Apps – Boredom Challenge Day 16


If a service you want to use does not have an SDK or library for the programming language you are using, chances are you will be using their REST API to interact with the system. REST API works – excluding the technical specifications – by putting our parameters in the http query string and/or the http header and using Http POST, GET, PUT and DELETE methods to tell the API what we wish to do. For example;

This is the code used if we wish to post something in Facebook for .NET SDK:

var postparameters = new
    access_token = fbAccessToken,
    message = TextBoxPost.Text
dynamic result = await facebookClient.PostTaskAsync("me/feed", postparameters);

Doing the same action RESTfully would be like this:

                       HttpRequestMessage request = new HttpRequestMessage(
                                new Uri(String.Format("https://graph.facebook.com/me/feed?access_token={0}&message={1}", fbAccessToken, WebUtility.HtmlEncode(TextBoxPost.Text))));

                        var response = await client.SendAsync(request);

As you can see, we set our parameters by adding them to the end of our URL. Actually, the former solution wraps the latter one for us, meaning that Facebook for .NET SDK gets the parameters from us and prepares the http requests itself to reduce our work amount.

Anyway, as I’ve said, we usually need to use REST APIs when we don’t have an SDK or library for the service we want to use. Judging from the code example above, it looks easy, right? Well, yes it is. As long as you have the documentation for it, you wouldn’t have any problems. Unless… you wanted to upload a file. Think of it, I said we put the parameters to the URL, but how do we do this with a file?

This is where multipart/form-data comes in, which is a standard way of encoding the files as byte arrays and sending them over with the http request. However, preparing our file as multipart/form-data is far from trivial, because it is very specific and requires you to delve deep into internet standards definitions to understand (if you wonder, try reading it: RFC 2388). And finally, if you wish to use it in Windows Store apps, another difficulty is that since the .NET classes are different for WinRT, the most common solutions on the internet are not usable in Windows Store apps. As of writing this, I was unable to find a working code sample.

Until now, of course. 🙂 In this article, we’ll see how we can prepare an http multipart/form-data request in Windows Store apps and upload a picture to Facebook with this method.

➤ It took me a complete (and very painful) day to put together the working code…

Uploading Photos and Videos to Facebook in Windows Store Apps – Boredom Challenge Day 15


In this article we’ll be continuing the Facebook integration in Windows Store apps, by looking at how we can upload photos and videos from a Windows Store app to Facebook.


Again, we’ll be using Facebook for .NET SDK, and to make things easy I’ll show how this can be done using the example app we’ve created in this article’s part 1. If you wish to follow this article and do it yourself, you can get the first part’s example here. If you don’t, the completed source code is at the end of this article.

➤ You can even tag them with your user’s friends…

Handling Suspension and Termination in Windows Store Apps – Boredom Challenge Day 14


It is not uncommon, especially in mobile app development, for our apps to be suspended or terminated in order to preserve the limited resources of a mobile device. Naturally, we would need to handle these situations in our apps, since there might be some final actions we need to do (such as saving our game’s progress, rolling back a not wholly completed calculation, or storing the contents of a half-filled form) before our app kicks the bucket. We would also need to handle the resuming in order to recover from suspension and termination.

Since Windows 8 emphasizes on being a mobile and power-saving OS, depending on the resources our app could get suspended when a user switches to another app or to desktop. Also, if the resources are low, the OS may deem it fit to outright terminate any suspended apps. Here’s a fancy diagram showing the Windows 8 app life cycle.


Apart from the power saving features of the OS, there is one more reason that makes it a good idea to handle suspension and termination in our apps, and this reason is the fact that there are no exit buttons. Our users can just close our apps with a signle flick gesture. We have no chance to give any warnings or prompts to our users like “Are you sure you want to exit?” or “Unsaved changes will be lost” and so on. So, you were uploading a file in the backround, or maybe you haven’t reflected a change you made in the app to the database yet. Well, tough luck, the user has just closed your app. Since the user (rightfully) doesn’t need to care about such things, it is our job to ensure the app overcomes these situations.

However, as you will see, it is quite easy to implement this feature, and in this article we will make a simple Windows Store app that handles suspension, termination and resuming.

➤ To suspend, or not to suspend. That is the question.

Integrating Facebook in Windows Store Apps Part 2 – Boredom Challenge Day 13


In my previous article, we’ve seen how we can sign in to Facebook, display our user’s data and share status updates in Windows Store apps. In this article, we will take our example app further by reading and displaying the user’s timeline and allowing the user to like/unlike posts, view a post’s comments and also add comments of his/her own.


We’ll start with the part 1’s completed app and continue from there. If you wish to go with the article step by step, you can get it here. If you’d rather not, jump to the end of the article for the completed source code. 😉

In this article, we will see how we can create Json classes for Facebook items, how we can read and display our user’s timeline, how we can like/unlike posts and how we can display and add comments. Apparently we have a lot of work to do.

➤ We’re basically making a Facebook client here…

Integrating Facebook in Windows Store Apps Part 1 – Boredom Challenge Day 12


I believe I don’t even need to tell you why it is a good idea to use Facebook in our applications. 🙂 Apart from the fact that it is the most widely used social media service, we can use it to allow our users to share something from our app (increasing our app’s publicity and usefulness), get posts – pictures – videos about a specific topic for our users (like latest news about a music group), or we can just use it as a user account system (as I’ve mentioned in one of my previous blog posts here). 1 For this purpose, we will use Facebook’s Graph API which allows us to do basically anything we could do on Facebook from inside our apps, and combine it with the Facebook SDK for .NET. Since there is a huge number of different functionalities provided by Graph API, I will be only showing about a handful of them (most used ones), so it is a good idea to keep the documentation for Facebook Graph API and .NET Facebook SDK at your side. 2 I’ve decided to split this article into several parts. In this first part, we will create a Windows Store app that signs the user in with a Facebook account, gets and displays our user’s information and finally allows our user to publish a status update. We’ll also see how we can get a long term access token from Facebook to keep our user signed in even after our app is closed.

➤ After these articles, just please don’t send tons of application requests to others…

Using Graphs and Charts in Windows Store Apps – Boredom Challenge Day 11


Graphs and charts are very effective tools for visualizing and summarizing data for our users. In apps they not only look cool, but also can be made interactive for further effectiveness. They are also an indispensible feature of financial apps or apps which need to show statistics.


In Windows Store apps, there isn’t a default XAML control in Visual Studio for graphs. However, we can easily use other libraries such as WinRT XAML Toolkit or Telerik RadControls for Windows 8 (there are many more). In this article we will create a sample app which uses different types of graphs and charts to show example data (pie chart, bar chart, line chart etc.) using WinRT XAML Toolkit.

➤ Bonus points for creating a “pie chart of my favorite bars” and “bar chart of my favorite pies”…