Monthly Archives: February 2013

UK Windows App Builder Reward Programme

www.appbuilder-rewards.co.uk

Show us that you’re a game changer and collect points every step of the way of your app building journey to redeem rewards like a brand new Lenovo Tablet 2. Create the next app phenomenon by building your original and unique Windows Store and/or Windows Phone app – and get recognised and rewarded for your hard work. Further, get bonus rewards for migrating your Windows Phone app to Windows Store and vice versa or incorporating one or more Windows Azure Services.

4 Simple Steps to Start Earning Points for your Awesome Apps

1. Register yourself on the UK App Builder Reward Programme website.

2. Build & publish your original & unique app.

3. Claim points by telling us about the app you’ve published and we’ll award points to you accordingly.

4. Redeem your points for a range of fantastic prizes and gadgets.

Build & publish an app between Monday 4th Feb 2013 and Tuesday 30th Apr 2013 to be eligible for points you can redeem for rewards. Here are the different ways you can earn points:

Points for Apps

Publish a New Windows Store App

50 Points

Publish a New Windows Phone App

25 Points

Bonus Points

Your Windows Store or Phone App incorporates one or more Windows Azure Services

+ 25 Points

Your app Windows Store app is migrated from Windows Phone or Windows Phone app is migrated from Windows Store

+25 Points

Example

For example, if you have built and publish a Windows Store app which was ported from an existing Windows Phone app and is hosted on Windows Azure, you will be eligible for up to 100 points for that app.

Windows Store App = 50 Points

+ Hosted on Windows Azure = 25 Bonus Points

+ Ported from Windows Phone = 25 Bonus Points

Total = 100 Points

To recognise your hard work, the more apps you publish, the more points you receive. The more points you get, the more exciting rewards you can claim. Here’s a brief selection:

Publish 1 Windows Phone App

Publish 1 Windows Store App

Publish 5 Windows Store apps + Windows Azure Services + Migrated from WP

Publish 10 Windows Store apps + Windows Azure Services + Migrated from WP

Get a XBOX360 Wireless Headset

Get a XBOX360 12 Months Gold Subscription

Get a Lenovo Tablet 2

Get a Two Night Deluxe Paris Cuisine Break for 2

In addition to the fantastic selection of rewards and experiences you can redeem using your points, we’re also offering some bonus rewards to give your journey to publishing great apps that extra boost.

For Windows Store Apps Builders

For Windows Phone Apps Builders

Receive one of the latest XBOX360 titles if you are one of the first 50 app builders who accumulate 150 points from publishing Windows Store Apps.

Receive a Nokia Lumia 620 Windows Phone 8 Handset if you are one of the first 20 App Builders who accumulate 100 points from Windows Phone Apps published (~windows phone apps)

Receive a copy Visual Studio 2012 Professional (with MSDN Subscription) if you are one of the first 5 app builders who accumulate 400 points from publishing Windows Store Apps.

Receive a Nokia Lumia 920 Windows Phone 8 Developer Handset if you are one of the first 10 App Builders who accumulate 200 points for publishing Windows Phone apps (~8 Windows Phone apps)

The UK App Builder Rewards Programme finishes on the 30th April so get your thinking caps on and we look forward to rewarding your awesome apps very soon.

UK App Builder Rewards Programme www.appbuilder-rewards.co.uk

Async and Await for Http Networking on Windows Phone

Using async and await for executing long-running operations such as networking calls has many benefits: your code is much cleaner and easier to maintain, the result is automatically marshalled back to the originating thread so you don’t need to use Dispatcher.BeginInvoke to get back to the UI thread, and exception handling is simpler to implement. Many of the APIs in the Windows Runtime on both Windows 8 and and Windows Phone 8 use this pattern. It’s the future way of programming asynchronous operations.

Unfortunately, the networking APIs in Windows Phone 8 have not been upgraded to support async and await. On Windows 8 Store Apps by contrast, HTTP networking APIs have been completely re-implemented to offer only async and await methods: the WebClient API has been removed and replaced with a more performant API, HttpClient, and HttpWebRequest has been revamped and now only offers Async methods. However, by use of some extension methods, we can enjoy the goodness of async and await with the WebClient and HttpWebRequest APIs on Windows Phone 8. It will work with Windows Phone 7.5/7.8 projects as well, as long as you add a reference to the NuGet Microsoft.bcl.Async package which adds in async and Task support for Windows Phone OS 7.1.

First, let’s look at HttpWebRequest. The GetResponseAsync extension method for this allow you to write code that is exactly the same as for Windows 8 – great if you are porting code between the two. Kudos to Mathias Shapiro, colleague at Microsoft, who developed this!

using System.Net;
using System.Threading.Tasks;

namespace WinPhoneExtensions
{
    public static class HttpExtensions
    {
        public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
        {
            var taskComplete = new TaskCompletionSource<HttpWebResponse>();
            request.BeginGetResponse(asyncResponse =>
            {
                try
                {
                    HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                    HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                    taskComplete.TrySetResult(someResponse);
                }
                catch (WebException webExc)
                {
                    HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
                    taskComplete.TrySetResult(failedResponse);
                }
            }, request);
            return taskComplete.Task;
        }
    }

    public static class HttpMethod
    {
        public static string Head { get { return "HEAD"; } }
        public static string Post { get { return "POST"; } }
        public static string Put { get { return "PUT"; } }
        public static string Get { get { return "GET"; } }
        public static string Delete { get { return "DELETE"; } }
        public static string Trace { get { return "TRACE"; } }
        public static string Options { get { return "OPTIONS"; } }
        public static string Connect { get { return "CONNECT"; } }
        public static string Patch { get { return "PATCH"; } }
    }
}

Using this, your HttpWebRequest calls now look something like this (don’t forget to add a using WinPhoneExtensions; import to the top of your class!):

        private async System.Threading.Tasks.Task GetSuppliers()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.odata.org/Northwind/Northwind.svc/Suppliers");
            request.Method = HttpMethod.Get;
            request.Accept = "application/json;odata=verbose";

            try
            {
                HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

                Debug.WriteLine(response.ContentType);
                // Read the response into a Stream object.
                System.IO.Stream responseStream = response.GetResponseStream();
                string data;
                using (var reader = new System.IO.StreamReader(responseStream))
                {
                    data = reader.ReadToEnd();
                }
                responseStream.Close();

                var feed = Newtonsoft.Json.JsonConvert.DeserializeObject<SupplierODataFeed>(data);
                SuppliersList.ItemsSource = feed.d.results;
            }
            catch (Exception ex)
            {
                var we = ex.InnerException as WebException;
                if (we != null)
                {
                    var resp = we.Response as HttpWebResponse;
                    var code = resp.StatusCode;
                    MessageBox.Show("RespCallback Exception raised! Message:{0}" + we.Message);
                    Debug.WriteLine("Status:{0}", we.Status);
                }
                else
                    throw;
            }
        }

…which is exactly the same as you would use in a Windows 8 store app.

We can do a similar thing for WebClient, though here the benefits are simply that the code you end up writing becomes so much cleaner. You don’t get the benefit of compatibility with Windows 8 since WebClient is not available on that platform.

I won’t list the code of the extension methods here, as it’s a bit lengthier than for HttpWebClient. Please download the samples attached to this post to get the extension methods code. When using the extension methods, you end up with lovely clean code like this:

string url = "https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name="
    + nameTextBox.Text + "&count=10";

var client = new WebClient();
string response = await client.DownloadStringTaskAsync(new Uri(url));

If you would like to watch me explaining this (and much, much more!) on video, check out the Networking video in the JumpStart series at http://aka.ms/i5qr0z.

Download the accompanying samples for the code!