Windows Phone 7 Tutorials: Maps, GPS and Accelerometer

Posted Wednesday, September 22, 2010 by

I've just uploaded the next three tutorials in the Windows Phone 7 Tutorials series. I ended up doing a batch of three this time due the simplistic nature of any tutorials involving features the emulator doesn't support well such as GPS and the Accelerometer.

Christchurch Earthquake Quake covers something very close to New Zealand at the moment, earthquakes. In the tutorial we cover retrieving earthquake data, parising it using Linq to Xml and placing quake markers on the new Bing Maps control.

Where am I? shows how you can use Reactive Extensions to emulate the GeoCoordinateWatcher class in the phone emulator.

Hold it Level again uses Reactive Extensions, this time to emulate the Accelerometer, we also use the VibrateController to provide tactile feedback to the user.

Relative Date Time Converter

Posted Saturday, September 11, 2010 by

Value converters are really what help make good applications become great, they plug into the binding infrastructure and help convert values appropriate to the model into values appropriate for the view. Being able to convert say a temperature from a simple integer into something like a SolidColourBrush gives a very easy representation of heat.

These work in all three of the xaml based frameworks, Windows Phone 7, Silverlight and WPF and because value converters are so compact they're also very testable.

A lot of sites represent days in a more interesting "5 minutes ago" format, so I set about throwing together a quick value converter for this, converting from a date to a string. I've become a big fan of using dictionaries of functions in this way for times when many if statements become too messy and a strategy style pattern is overkill.

public class RelativeDateTimeConverter : IValueConverter

{

    private const int Minute = 60;

    private const int Hour = Minute * 60;

    private const int Day = Hour * 24;

    private const int Year = Day * 365;

 

    private readonly Dictionary<long, Func<TimeSpan, string>> thresholds = new Dictionary<long, Func<TimeSpan, string>>

    {

        {2, t => "a second ago"},

        {Minute,  t => String.Format("{0} seconds ago", (int)t.TotalSeconds)},

        {Minute * 2,  t => "a minute ago"},

        {Hour,  t => String.Format("{0} minutes ago", (int)t.TotalMinutes)},

        {Hour * 2,  t => "an hour ago"},

        {Day,  t => String.Format("{0} hours ago", (int)t.TotalHours)},

        {Day * 2,  t => "yesterday"},

        {Day * 30,  t => String.Format("{0} days ago", (int)t.TotalDays)},

        {Day * 60,  t => "last month"},

        {Year,  t => String.Format("{0} months ago", (int)t.TotalDays / 30)},

        {Year * 2,  t => "last year"},

        {Int64.MaxValue,  t => String.Format("{0} years ago", (int)t.TotalDays / 365)}

    };

 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

        var dateTime = (DateTime)value;

        var difference = DateTime.UtcNow - dateTime.ToUniversalTime();

 

        return thresholds.First(t => difference.TotalSeconds < t.Key).Value(difference);

    }

 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

        throw new NotSupportedException();

    }

}

<TextBlock Style="{StaticResource PhoneTextSubtleStyle}" Text="{Binding Created, Converter={StaticResource RelativeDateTimeConverter}}" />

Windows Phone 7 Tutorials: Isolated Storage and User Controls

Posted Saturday, September 04, 2010 by

The next two tutorials in the Windows Phone 7 Sample Applications have just been uploaded. They've taken a little longer to get out as I've started work on my first actual application for the Windows Phone 7, a client for the Stack Overflow family of websites. I'm also developing the example applications out of series because I'm waiting for the offical maps controls before developing applications using that.

Quick Mail allows you to store email templates to allow you to send out standard emails quickly. It demonstrates user of Isolated Storage, the Application Bar and Page Navigation.

Snowfall displays an animated snowfall you can interact with using touch events. It demonstrates creating User Controls and Manipulation Events.

Hope you enjoy.

Two more WP7 tutorials

Posted Tuesday, August 17, 2010 by

I've just upated the Windows Phone 7 Example Applications list with the next two applications. Both are a little longer, and little more complex as I try to introduce some more features. Given that both also use Expression Blend to do some of the work I've included videos on how to use Blend.

Count Me In is an app to help you keep your place while counting, it introduces the MVVM pattern, commands, binding and behaviors.

Reaction Time tests your reaction speed and brings into play the Silverlight Visual State Manager, transforms and transitions.

Hope you enjoy.

Page 5 of 14<34567>

Professional Windows App Development