Windows Phone 7 Series Developer Experience

imageToday at Mix10, Microsoft released the developer tools for Windows Phone 7 Series.  I’m excited about seeing what apps will get built now that there is general availability of the Windows Phone dev tools.  That’s general availability, as in for everyone!

Charlie Kindel (if you aren’t, you really should follow him) was nice enough to get me access to the tools about 3 weeks ago, despite the fact that he and his team have had plenty on their plate since going public with WP7.  I have spent some time in the last few winks building and tinkering.

A little background on my dev skills.  I can write basic applications, and have been known to favor Python when trying out new ideas.  I have dabbled a bit with our ASP.NET MVC (MVC v2 just released – way to go guys!!) and taught myself enough C# to be dangerous.  What coding I do, I do for fun and in my free time.  I call it my nocturnal nerdiness, and have been logging some of my projects using the n00bnotes tag.  Prior to 3 weeks ago, I had never written one line of Silverlight (or WPF for that matter) code, nor any XAML.  I was really excited to have the opportunity to build apps for this mobile platform, as I once tried to get along with iPhone development, and while it’s clear that Apple has created tools that developers seem to love, I couldn’t get along with ObjectiveC.  That’s a me issue, and not a statement about ObjectiveC.  I get along famously with Python, but me and Ruby are not friends.  That’s just the way my brain works.

imageWith that as a preamble, I wanted to share what I have created in just the last 3 weeks, working largely in what spare time I could find when not doing my day job or dealing with an recalcitrant 8 month old girl who refuses to sleep.  The main thing I want people to take away from this is that it in incredibly easy to built apps for Windows Phone 7.  If I can figure it out, anyone can.  The team has delivered a great development experience built on top of Visual Studio Express.  When you fire up the development environment, everything you need is there and you are ready to go.  It was a pretty painless experience to get the environment up and running, and it includes templates for Silverlight apps as well as XNA games.  While I have only been able to deploy to an actual phone once, the emulator felt like a software version of the phone.

Over the next few days of Mix10, I am going to put up a few posts about my experiences with the development tools, highlighting some of the blockers I hit, how I solved them, and for some of them, how I should have solved them, which I eventually fixed during code refactoring.

In the meantime, I wanted to share a link to the current version of the code.  This is my FriendLinks application, built for Windows Phone 7 [UPDATE: 3/24/10 – oh the joy of forgetting to remove commented code with your twitter pass.  Ooops…code updated.]  You will need the development tools in order to open, edit view.  The only disclaimer I make is that the code works.  Not all of it is pretty, and in some places I haven’t gone back to fix things I fixed elsewhere (i.e. walking XML for Bit.ly versus for Twitter).

This specific post is about some of the things that gave me the biggest problems in getting started.  The app that I built is pretty simple.  It’s meant to allow you to connect to Twitter, pull down your friend timeline, and parse the timeline looking for URLs sent by people you follow.  I use Twitter for content discovery, and this is my ultimate time waster app.  When you click on a link the listbox, some additional calls are made via the Bit.ly API, and the TweetMeme API, to get additional information like the number of retweets that article has, the title of the page referenced and the number of clicks as tracked by Bit.ly.

Making Async Calls to Web Services

Wow, what a huge pain this was for me to figure out.  When you do some web searching about how to connect to web services in C#, you will invariably find yourself staring at content about WebClient class.  I couldn’t make this work for me, and now that I am 3 weeks into it, I can’t remember what the specific issue was.  Something about Twitter not doing Basic Auth correctly, and needing to set the username and password in the header, necessitating the use of NetwrokCredentials.

In any event, I had to use the HttpWebRequest.  This is where things got challenging for me, since I had never done any async programming in C# or Silverlight.  [Apologies for the wonky formatting in the code samples, but I have a narrow blog and the style isn’t doing auto wrap]

public void GetStatuses()
{
    NetworkCredential nc = new NetworkCredential { UserName =

                          _username, Password = _password };

    string url = http://twitter.com/statuses/friends_timeline.xml?

                               + "count=" + _count + (_sinceID > 0 ?

                               "&since_id=" + _sinceID : "");
    System.Net.HttpWebRequest request =

                              (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "GET";

    if (_username != null && _password != null)
    {
        request.Credentials = nc;
    }

    IAsyncResult token = request.BeginGetResponse(

                                  new AsyncCallback(GetStatusesCallBack),

                                  request);

}

Sorting out how to correctly get the async call done, and then set up the callback, was where I completely threw a rod.  Basically, you need to set up a web request to happen on its own thread, and then you need to assign a delegate function to process the response.  Figuring out how to do this took me 2 days, mostly because I didn’t know what questions to ask, or what terms to use when searching online, and in this specific case, only code that I did discover was useless for me because I was trying to learn something new and didn’t understand the samples I found.

Looking at the call, you may notice that I am using the REST API from Twitter, and setting the since_id and count variables to ensure that I am getting the data that I want.  This is so I can reuse this function to make subsequent calls while the app is running and only get the new tweets.  Twitter makes things pretty easy to get the data you want.

Here’s how you handle the async callback function to actually issue the HTTP request and process the data which comes back:

public void GetStatusesCallBack(IAsyncResult result)
{
    bool foundNewSinceID = false;

    try
    {

        WebResponse response = ((HttpWebRequest)result.AsyncState)

                                       .EndGetResponse(result);
        StreamReader reader = new StreamReader(response

                                       .GetResponseStream());
        string responseString = reader.ReadToEnd();

  • Pingback: Great news from MIX: Windows Phone development, Silverlight 4 RC available! | DavideZordan.net

  • Pingback: Many Niches » Blog Archive » Windows Phone 7 App Deployed

  • http://blog.donnfelker.com Donn Felker

    Thanks for posting the example. Much appreciated.

  • http://john-sheehan.com/blog John Sheehan

    That's a pretty good lookin' dude atop your list.

  • joeldm1971

    Subscribed … looking forward to the follow up posts :-)

  • http://www.manyniches.com brandonwatson

    That *is* a good looking dude. Hopefully I will get a chance to hang out with you and your developer peeps here at Mix10.

  • http://www.manyniches.com brandonwatson

    You bet Donn. I will be posting more later this week. I don't claim to be the best developer in the world, and my code may not be 100% “right” but I hope that what people take away from this is that it really is that easy to build apps for this platform.

  • http://www.manyniches.com brandonwatson

    Thanks Joel – more Windows Phone 7 posts this week.

  • http://www.manyniches.com brandonwatson

    Thanks Joel – more Windows Phone 7 posts this week.

  • Aurojit Panda

    Hi
    As a fellow beta tester I just had one comment about your article.

    You say
    “Something about Twitter not doing Basic Auth correctly, and needing to set the username and password in the header, necessitating the use of NetwrokCredentials.”

    The problem really isn't with Twitter, the spec defines that Basic authentication is carried out by sending a Base64 encoded “username:password” string in the HTTP headers as a part of an authenticated request (http://en.wikipedia.org/wiki/Basic_access_authe…).

    As far as I know your problems are caused by a bug in WebClient, where webclient does not correctly process the Credentials property. I am sure they will fix it in the coming days, but the current problem with WebClient and credentials extends to all services which support basic authentication, not just Twitter.

  • http://www.manyniches.com brandonwatson

    @Aurojit – thanks for making that clear. I will go back in and fix the post this evening. I also found another bug which I plan to post later tonight once I get clearance from the dev team, but for this use case, it's important to know it's a bug and how to handle.

  • http://www.bigapplechannel.com/ Andrew Sansone

    How much competition will Windows 7 Series cell phone be for iPhone? It was a trending topic all day on Twitter http://bit.ly/duxQxg

  • http://www.manyniches.com brandonwatson

    I'd like to think that Windows Phone 7 will get develoeprs excited about building new experiences and applications, and customers excited about experiencing their mobile phone in a new, and more personal, way. Time will tell, but I am a believer.

  • Pingback: Stumpy » [Mix10] Une soudaine envie de développer sur mobile ?

  • Pingback: Many Niches » Blog Archive » What Do You Call One Search Result From StackOverflow?

  • Ibrahim

    Don't you think it would be better to load an XDocument or XElement with an XmlReader instead of reading the stream to end first. This would use the advantages of using a stream.
    Thanks for posting your experiences.

  • Pingback: Windows Phone 7 Developers Hub coming up in Helsinki — FinnMob

  • Pingback: Thank You Sponsors