Technology Blog

Archive for the ‘Windows Phone 7’ Category

Windows Phone 7–Shake It Like You Mean It!

Thursday, February 24th, 2011

Shake Gesture Library Now Available

 

image_thumb27When developing applications for Windows Phone 7 you have access to vast amounts of information about the device through its sensors. One of these is the accelerometer  which can give you information on where the device is moving within 3D space, this can be utilised in many different ways, the most prominent of these being in gaming.

There are however many other ways to use this information, you may want a user to be able to shake their device to update their  twitter list instead of pressing a button, or many other implementations like this.

To make this easier to do you can now use the Shake Gesture Library which is available to download from the AppHub  and there is also a great overview on the Windows Phone Developer Team Blog.

 

The Magic Eight Ball App

 

To show how easy this is to use, lets write a simple app, I want to  ask my eight ball a question, shake my device and have an answer appear.

  • Create a new Windows Phone 7 project in Visual Studio:

NewProj

  • Create a UI which has an image of an eight ball with a TextBlock hovering over the top to show the answer.

 MagicEightBall - Microsoft Visual Studio (Administrator)

  • Add initialisation of a List<string> to hold our responses

MagicEightBall - Microsoft Visual Studio (Administrator) (2)

  • In the Loaded event of our page add the handling for the shake gesture using the Shake Gesture Library.

MainPage

 

That’s our App done, your Windows Phone 7 can now impart you with the knowledge of the Universe. Go Shake!!

Download the sample

Silverlight for Windows Phone Toolkit–February 2011

Monday, February 21st, 2011

A refresh to the Silverlight Toolkit for Windows Phone was released a few days ago which contained along with a few bug fixes some cool new features:

  • TiltEffect
    • Gives you the ability use an attached property to apply a cool tilt animation to most UI Elements.
  • PerformanceProgressBar
    • A highly performant version of the built in ProgressBar
  • VB.Net samples
    • Sample projects for Visual Basic developers

You can download the bits HERE

Creating Striking Mapping Applications On Windows Phone 7 Using Bing Maps and CloudMade

Thursday, January 20th, 2011

After presenting at the Bing Maps User Group recently in which i discussed how to implement a custom mapping tile layer using the Deep Earth Silverlight control (http://deepearth.codeplex.com) and the Cloudmade mapping service (http://www.cloudmade.com) I began thinking about using this method to create a mapping application for Windows Phone 7 which more fits in with metro theme.

image

To begin with let’s have a look at the app we are going to build, as you can see the tiles being displayed are completely different to the standard Bing Maps tiles. Not only are you able to choose from thousands of pre-set map styles but you can also create your own too!

Ok lets kick off building this, to begin you are going to need to head off to the Bing Maps Portal to sign up and create yourself an application key. Once you have done this head off to CloudMade and create yourself a developer account. We are now in a position to actually write some code, create a new WP7 project and add a reference Microsoft.Phone.Controls.Maps.dll. We can now add some code to our MainPage.xaml as below.

Add two namespace statements to the top of the page:

xmlns:maps="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"

 

xmlns:core="clr-namespace:Microsoft.Phone.Controls.Maps.Core;assembly=Microsoft.Phone.Controls.Maps"

 

Then add a map control to the page also, setting the map mode as below:

 

<maps:Map CredentialsProvider="{Enter Your Bing Maps Key Here}"

 

VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" Name="map1" >

 

    <maps:Map.Mode>

 

        <core:MercatorMode></core:MercatorMode>

 

    </maps:Map.Mode>

 

</maps:Map>

 

If you run the application at this point you will not see a great deal of anything, in effect what we have done is turn off the standard Bing Map tiles. We now need to create our custom tile provider to serve the tiles from CloudMade to our app using the code below:

public class CloudMadeTileSource : TileSource

 

{

 

    private const string _tilePath= "http://{S}.tile.cloudmade.com/{creds}/{style}/256/{Z}/{X}/{Y}.png";

 

    private readonly Random _rand = new Random();

 

    private readonly string[] TilePathPrefixes = new[] { "a", "b", "c" };

 

    public string CloudMadeCredentialsProvider { get; set; }

 

    public string CloudMadeMapStyleId { get; set; }

 

    public override Uri GetUri(int x, int y, int zoomLevel)

 

    {

 

        string url = string.Empty;

 

        string prefix = string.Empty;

 

        prefix = TilePathPrefixes[_rand.Next(3)];

 

        url = _tilePath;

 

        //Randomize to different OSM Servers based on URL prefix

 

        url = url.Replace("{creds}", CloudMadeCredentialsProvider);

 

        url = url.Replace("{style}", CloudMadeMapStyleId);

 

        url = url.Replace("{S}", prefix);

 

        url = url.Replace("{Z}", zoomLevel.ToString());

 

        url = url.Replace("{X}", x.ToString());

 

        url = url.Replace("{Y}", y.ToString());

 

        return new Uri(url);

 

    }

 

}

Here we are inheriting from the TileSource class and overiding the GetUri method which gives the mapping control the uri to the image file for a tile on the map. this is done by taking a URL which has placeholders for the important pieces of information the CloudMade system requires which is described very well on the CloudMade site. we also have properties for setting the CloudMade credentials and style id so these can be set in xaml.

We can now add a a reference to this class in our xaml as so:

xmlns:local="clr-namespace:StylisedMap"

 

Then add the TileSource to the map in our xaml:

 

<maps:Map CredentialsProvider="{Enter Your Bing Maps Key Here}"

 

    VerticalAlignment="Stretch" HorizontalAlignment="Stretch"

 

Grid.Row="1" Name="map1" >

 

    <maps:Map.Mode>

 

        <core:MercatorMode></core:MercatorMode>

 

    </maps:Map.Mode>

 

    <maps:MapTileLayer>

 

        <maps:MapTileLayer.TileSources>

 

            <local:CloudMadeTileSource

 

CloudMadeCredentialsProvider="{Enter Your CloudMade Key Here}"

 

CloudMadeMapStyleId="{Enter Your Chosen Map Style Id Here}" />

 

        </maps:MapTileLayer.TileSources>

 

    </maps:MapTileLayer>

 

</maps:Map>

If you now build and run the application you have a working Bing Map Windows Phone Application with custom map tiles. Enjoy!!

You can download the sample project here.

Windows Phone 7 And IPhone - Sharing Code

Monday, September 13th, 2010

A Match Made In C#

 

I got very exited when i began learning Objective-c, the prospect of being able to write native IPhone apps was very attractive to me. I don’t know why i got exited but like a large proportion of developers (based on the 200k + apps in the store) i did and i set out to conquer the beast.

I am now two commercial apps in using Objective-c and XCode and i am starting to feel like there must be a better way of going around this, With Windows Phone 7 on the horizon and a large user base of the Android platform out in the wild i want to get as close to write once and reuse everywhere as is physically possible. But how is this possible when each platform has its own Language, Framework and IDE?

Enter Mono, this excellent project which allows you to write, build and deploy .net code to non Windows platforms which is spearheaded by Miguel de Icaza means you can now write a large proportion of your code once and it can be reused on the 3 mobile platforms.

To give you an example here is a walkthrough of how you may go about doing this:

note: To follow the below guide you will need a Windows machine with Visual Studio 2010 and the Windows Phone 7 Beta tools installed, also a Mac with the IPhone SDK, MonoDevelop and MonoTouch installed.

 

Open Visual Studio 2010 and create a new Windows Phone 7 project, I have called the project “CodeSharing.WP7” and the solution “CodeSharing”.  I have created this solution in my drop box folder to share with the Mac i am going to be using but when creating a real application this would be done using a version control system.

New WP7 Project

Add a Button called “btnSayHello” and a TextBlock called “txtHello” to the MainPage.xaml.

WP7UI

Right click on the solution and add a new project, select Silverlight Class Library and make sure you have .Net 3.5 selected. When given the Option choose Silverlight 3. I have called this project “CodeSharing.Shared”.

AddSharedProject

Add a class to the new project called “HelloMobiles” and add the code to the class which is below:

ShaedCode

Now in your Windows Phone 7 project add a reference to the new Class Library then add a click handler to the btnSayHello by double clicking it in the designer. You can then add the code below:

WP7ClickHandler

If you now hit F5 and click the button you will see that it has pulled the text through to the TextBlock:

WP7EndResult

So nothing special so far, we have created a very basic Windows Phone 7 Hello World application, so onto the good bit.

If we now open up the solution in MonoDevelop on the Mac you will see the following:

Screen shot 2010-09-13 at 10.37.20

The solution has opened fine but the Windows Phone 7 project has not loaded, This is not a problem as we are going to be adding an IPhone project. Right click on the solution and add a new project. Under the C# menu select IPhone Window-based project.

Screen shot 2010-09-13 at 10.44.33

Once the IPhone project is created add a new IPhone View with Controller called “MainViewController” and then double click on the MainViewController.xib to edit the view in Interface Builder. I am not going to go into here how you create the UI but there is a great tutorial here. You will end up with a view looking like below and outlets for the UIButton and the UITextField specified on your view controller.

Add a reference to the compiled dll of the CodeSharing.Shared project (you cannot add a reference to the project itself as MonoDevelop says that the project is incompatible) and add the following code to the ViewDidLoad method of the MainViewController code behind.

 

Screen shot 2010-09-13 at 11.17.57

And make the FinshedLaunching method in the Main class look like the following:

Screen shot 2010-09-13 at 11.18.35

Set the IPhone project as the start up project and you are good to go.

 

This method is definitely not the best way going about this but it does give you an idea on how we can reuse code between many different mobile platforms using C# and Visual Studio 2010. I am particularly looking forward to getting my hands on MonoDroid so i can start writing Android apps too.

 

Download the sample code here.

Syndication

Links

Archives

June 2013
M T W T F S S
« Feb    
 12
3456789
10111213141516
17181920212223
24252627282930

Other