4MK Mobile Dev Blog

Adventures in Mobile Development

Quick Tip: No really Codeplex, I am who I say I am

Working on open source projects can be one of the most rewarding things that you can do as a developer.  Code things on your own schedule, to your own specifications, what more can you really ask for?  While nobody will mistake Microsoft for a not-for-profit, they have done quite a few things to help out developers working on Open Source projects, perhaps the biggest one is Codeplex.  Codeplex allows you to host your project, free of charge while also including a few great features like discussion forums, issue tracking, and perhaps most of all source control.

When working with Codeplex, my source control flavour of choice is Team Foundation Server.  Mostly this is because of how well it integrates with Visual Studio…for the most part.

The one thing that used to drive me nuts about using TFS in Visual Studio was that every single time you open the project (or use the super useful Open in Blend functionality when dealing with WP7 projects) it asks you again for your username and password with no visible way for you to save your credentials.  After months of complaining about it, I finally decided to do some research and find out how to fix this.

Here are the steps (I`m using Windows 7, but I`d imagine it can`t be that much different on Vista or XP):

1) Within the Control Panel (Start->Control Panel) Search for Manage Credentials and open up the applet.

Credential Manager

2) At the top of the page you should see Windows Credentials and a link to “Add a Windows Credential” click that now.

 

AddWindowsCredential

3) Use tfs.codeplex.com as the network address then enter in your credentials exactly as they are on codeplex, including the domain (ie “snd”)

CodeplexCredentials

4) Hit OK and you should be good to go.

While this tip won’t solve world hunger, hopefully it’ll make your experiences writing open source software a little more enjoyable.

Demystifying Threading with web requests

WebThreadsIf you’ve been building Windows Phone 7 apps that display information that is retrieved from the web (yes I’m talking to you 300 twitter client authors), you may have run into the problem of some unresponsiveness during start up. While I can’t say this will definitely solve your problems, in this post I’m going to explain how I solved it for my app.

One of the apps that I’m building pre-fetches a fair amount of data during start up.  To hide this from the user, I’ve added a loading screen that has a progress bar that animates across the screen.  I started noticing that, even on the emulator, the loading of the application got a bit choppy.  Rather than going through the details of me banging my head against the wall trying to figure out why this was happening, I’ll cut right to the chase…

The DownloadAsyncCompleted Handler of the WebClient class executes on the UI thread.

As I’m sure you all know, anything that runs on the UI thread, can cause your UI to become choppy, or even unresponsive and whenever you can safely move processing off the UI thread, you probably should.

What was happening in my case was that processing the response from the WebClient meant that the application couldn’t update the UI until it was done, causing the choppiness. While this sounds like it could be a huge headache, the solution is actually quite simple:

When you can, pre-fetch data using HttpWebRequest instead of WebClient.

Since, the response handler of HttpWebRequest executes in it’s own thread, your UI is free to be as responsive as ever.

If you’d like to see what I’m talking about in code, you can check out a quick sample (as seen in the screenshot above) here.

Quick Tip: Position a map based on a collection of Pushpins

CenteredAndZoomedMap

Let me start by apologizing for the severe lack of posts over the last little while, things have been changing rapidly here at 4MK Mobile, and while it caused my absence, it’s also going to give me the opportunity to write many more post (both quick tips, and full on, multipart tutorials).

Expect a blog post on what’s been happening this week, for now all I’ll say is I’ve never been more excited or happy to work!

Anyways, on to the tip …

Recently I came across the question of how to set the bounds of a Bing Maps control based on a collection of Pushpins.  Having gone through the headache of building my own solution to this problem before finding that MSFT has provided a helper utility just for this case, I thought I’d share it here.

Let’s say that you have retrieved a collection of points (named “Points”) from your web service, you’ve placed them on the map (named “map”, and your now looking to change both the center and the zoom level of your map control to show only these points, the code is rather simple:

var x = from l in Points
select l.Location;
map.SetView(LocationRect.CreateLocationRect(x));       

Hopefully this can save a few of you a couple hours.

Quick tip: Playing a streamed mp3

Capture

In an attempt to start changing this blog from nothing but a platform for me to spout off about projects, or talking about contests :) , and actually try to provide some value to readers, I’m starting a new category on the site called “Quick tips”.  When I can, I’ll be posting small bits of code that I think would be helpful.  If you have a question on how I’ve done anything in the applications I’ve written (more to be posted soon), please feel free to ask, and I’ll answer what I can.

On my last post, Ishmeet asked if I could shed a bit of light on how I was able to play the audio clips in the PhoneTree application, so here goes:

One of the best things about having both Silverlight and XNA at your disposal when developing for WP7 is that you can use the XNA pieces within your Silverlight app when ever you want.

For the MediaPlayer functionality you’ll need to add the Microsoft.Xna.Framework.dll reference.  Once you’ve done that, use the following code:

MediaPlayer.Stop();
Song song = Song.FromUri("Sample Song", new Uri(txtUrl.Text ) );
MediaPlayer.Play(song);

If you try and run the code at this point, it will play, but the application will also crash complaining that FrameworkDispatcher.Update has not been called.  After looking around for a while I finally found a post by Danny on the XNA Team explaining the problem.  He did a better job explaining it then I can so you can read it here.

Basically you’ll need the following class:

 public class XNAAsyncDispatcher : IApplicationService
    {
        private DispatcherTimer frameworkDispatcherTimer;

        public XNAAsyncDispatcher(TimeSpan dispatchInterval)
        {
            this.frameworkDispatcherTimer = new DispatcherTimer();
            this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);
            this.frameworkDispatcherTimer.Interval = dispatchInterval;
        }

        void IApplicationService.StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
        void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }
        void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }
    }

and then add it to the Application Lifetime Objects like so:

this.ApplicationLifetimeObjects.Add(new XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));

Once you’ve done that, your good to go.

Here is a sample showing off this technique.

Screencast of the Alpha version of Phone Tree

Let me start this post of by apologizing for my hideous microphone, I’ll make sure to pick up a much better one before I post any more videos on this blog :)

With that out of the way, here is a quick video of my entry to Twilio’s WP7 Netbook contest.  Keep in mind that I’ve only been working on it for four days now and that it will certainly get some much needed polish in the near future.

Currently it will allow you to send out your updates using either SMS or Phone and text to speech, as well as allow for your contacts to leave a message for you if they are set up as Phone contacts.

Let me know what you think, or any suggestions you may have in the comments bellow.

« Newer PostsOlder Posts »