Integer to string formatted as currency

Since it has been a while since the last post, I thought I would give a quick trick that shows the power of string.Format().

Say you got an integer “price” that you want to display in a string, but formatted as currency. You can do this very easily using string.Format():

int price = 2795000;
string priceStr = string.Format("The price is {0:C}", price);


priceStr would now be “The price is 2.795.000,00 kr” if the Culture was set to Swedish. If you want to “lock” the culture do like this:

int price = 2795000;
string priceStr = string.Format(new CultureInfo("en-US"), "The price is {0:C}", price);


That would format it as $ instead, giving priceStr the value “$2,795,000.00″

I also found a great list of (all?) the possible ways to format using string.Format() that I can highly recommend here:

Free Windows Phone 7 Toolkit e-book

Today I found a new e-book, that I just have to recommend, which explains the Windows Phone 7 Toolkit in depth. It looks really good, covering most controls in detail, and giving some nice examples. This will probably be a good complement to the examples that comes with the toolkit. The book is written by Boryana Miloshevska, an experienced software developer. It can be downloaded for free here:

Update: I can also recommend the free e-book “Programming Windows Phone 7″ by Charles Petzold ( which is also a great resource for WP7 development.

Multiple selection in Listpicker

I thought I would write a quick how-to about how to use the ListPicker for selection of multiple items.

First create a ListPicker in XAML with a few deafult strings like below:

<toolkit:ListPicker Name="myListPicker" Header="Countries" FullModeHeader="CHOOSE COUNTRIES"
SelectionChanged="myListPicker_SelectionChanged" SelectionMode="Multiple">

Notice the attribute SelectionMode=”Multiple”, which sets that the user can select multiple items, we also added an event handler for the SelectionChanged event.
Note: To use < sys:string > you need to add xmlns:sys=”clr-namespace:System;assembly=mscorlib” at the top in the < phone:PhoneApplicationPage> tag.

Now in the construct for the page, add the following code:

myListPicker.SummaryForSelectedItemsDelegate = SummarizeItems;

This assigns the function SummarizeItems to summarize the string to be shown for the selected items. Let’s create that function now:

private string SummarizeItems(IList items)
   if(items != null && items.Count > 0)
      string summarizedString = "";
      for(int i = 0; i < items.Count;i++)
         summarizedString += (string)items[i];
         // If not last item, add a comma to seperate them
         if(i != items.Count - 1)
            summarizedString += ", ";
      return summarizedString;
      return "Nothing selected";

This function gets an IList with all selected items and we just loop through them and add to a string with a comma between every selected item. Then we return the string, which will be shown when the list picker is in normal mode.

Hope you enjoyed this quick how-to!

ListPicker inside ScrollViewer WP7 Toolkit Mango

Today I was going to add a ScrollViewer to my project, the problem is that inside the ScrollViewer control I had a ListPicker. Shouldn’t be a problem, right?

But unfortunatly that seems to be a known issue in the August release of the WP7 Toolkit. To resolve this I found a solution over att the Silverlight Toolkit website that seems to work. What you have to do is edit the source of the WP7 toolkit, more precisly the file ListPicker.cs. Here is how you do it:

On line 881, change the following:

if (IsValidManipulation(e.OriginalSource, e.ManipulationOrigin.X, e.ManipulationOrigin.Y)
     && 0 < Items.Count)
   if (Open())
      e.Handled = true;

To this:

Point point = new Point(e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
if (e.ManipulationContainer != e.OriginalSource)
   var trans = e.ManipulationContainer.TransformToVisual((UIElement)e.OriginalSource);
   point = trans.Transform(point);
if (IsValidManipulation(e.OriginalSource, point.X, point.Y) && 0 < Items.Count)
   if (Open())
      e.Handled = true;

Credit for this goes to Drachen23 at CodePlex.


Note: The solution also mention, to handle the Tap event of the ListPicker does not seem to work, it correctly opens the ListPicker, but does not always react when user wants to close it in Expanded Mode.

Changing font size in panorama title and headers

So, it was a while since the last time, unfortunately had too little time to develop apps, but hoping that will change soon. I’m currently working on upgrading my apps to Windows Phone Mango, so they support multitasking and some cool new features such as multiple live tiles.

But now I’m going to show how to change the panorama title and the headers of the panorama items in WP7, something that took me a while to figure out, because changing the fontsize property doesn’t do the trick.

Instead you first need to make a custom template in your App.xaml file in the <Application.Resources> tag, like this:

        <datatemplate x:key="SmallPanoramaTitle">
                <textblock text="{Binding}" fontsize="50" margin="0,70,0,0" />

This creates a templete we can use to change the font size of either the title (like in this case) or the headers. Now set the TitleTemplete property of the panorama control in your MainPage.xaml file, like this, to apply the template:

<controls:panorama title="my application" titletemplate="{StaticResource SmallPanoramaTitle}">

This will give the following result when running the application:


You can also choose to change the size of all headers by setting the HeaderTemplate instead of TitleTemplate for the panorama control:

<font size="2"><controls:panorama title="my application" headertemplate="{StaticResource TinyPanoramaTitle}"></font>

Giving the following result if you reduce the font-size alittle or make another template for the headers (like I have done here):


You can also change individual headers by setting their HeaderTemplate instead of the panorama controls HeaderTemplate, like in the screenshot below. By changing the template you can change the most properties of the title and headers, like moving it using the margin property:


Hope you enjoyed this quick tutorial! You can download the project here (for Windows Phone 7.1 Beta 2).

Check if user has dark or light theme in WP7 Silverlight (C#)

Thought I would share a small piece of code to check if the user is using a light or dark theme, as it seems some developers forget that you can actually have a white theme too ;)

The following piece of code will make a boolean that is true if the user has the dark theme and false if he/she has white theme:

bool darkTheme = ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible);

Then use the boolean to change the theme of your application when necessary. It is also a nice idea to make a static function you can reuse to check whether the user has dark or light theme.

Note: There is also a “PhoneLightThemeVisibility” value in Application.Current.Resources dictionary that can be used in the reversed manner from what is shown above.

Different coordinate format in different regions (Problem with Svenska Platser)

Many who has tried my new application Svenska Platser has noticed that the GPS location might not work. After a lot of debugging I noticed what was wrong.

When you your GeoCoordinate watcher like this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    GeoCoordinateWatcher gcw = new GeoCoordinateWatcher();
    gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);
void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
     string Latitude = e.Position.Location.Latitude.ToString();
     string Longitude = e.Position.Location.Longitude.ToString();

Apparently the format of decimals differ depending on the phones region settings. For example the result from the code above would with different region settings be:

Region Format set too Swedish:
Latitude: 59,35635
Longitude: 18,296474

Region Format set too English(UK):
Latitude: 59.35635
Longitude: 18.296474

As you can see the decimals is denoted by a comma in Swedish region settings, while with English(UK) it is denoted by a dot. And as my API wanted only dot, it didn’t get any results for the users with Swedish region settings. So this might be something to take into count when you are developing an application using the GeoCoordinateWatcher and converting the coordinates to strings.

An easy fix is simply replacing the comma with a dot:

void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
     string Latitude = e.Position.Location.Latitude.ToString().Replace(',','.');
     string Longitude = e.Position.Location.Longitude.ToString().Replace(',','.');

An update for Svenska Platser has now been submitted to marketplace for certification and will hopefully appear in marketplace soon!

Getting WP7 Unique Device ID

As I promised in my last post I would show how to get the Unique ID of a Windows Phone 7. This is a really simple process. You will need this ID for example when running AdMob in test mode for specified devices.

byte[] myDeviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
   string idAsString = Convert.ToBase64String(dui);

This number is Unique for the device and is not changed even if the phone changes owner or the OS is updated.

You can also use Microsoft.Phone.Info.DeviceExtendedProperties to get more information about the phone and application. For example you can get the manufacturer of the phone:

string manufacturer = (string)Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceManufacturer");

or how much the memory the application is currently using:

long memoryUsage = (long)Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");

Read more…

Using AdMob in WP7 Silverlight Applications

I recently read that Google released a beta for AdMob in WP7 apps. That was great news for developers like me that can’t use Microsoft pubCenter that is US only. So I thought as a first real post here I would write a little tutorial on getting started with the SDK.

Download and install the SDK
First you have to create an account at (or login using an existing account) and add a new WP7 app to the AdMob account. You will then be presented with the link to download the AdMob WP7 SDK.

Add references
First off, add a reference to “Google.AdMob.Ads.WindowsPhone7.dll” that came with the SDK.

Add reference

In your XAML file add the following two namespace declaration at the top:


Add the BannerAd control
To add the Ad Control, just add the following piece of code:

<google:BannerAd Name="MyAd" AdUnitID="n50g7fd69132t92" />

The AdUnitID is your Publisher ID presented on the AdMob website. If you run the project you will now get an ad shown. (No that is not my publisher ID )

Run AdMob in test mode
But to avoid getting impression counted while developing the application you might want to use test ads, you can do that by changing the ad code to:

<google:BannerAd Name="MyAd" AdUnitID="n50g7fd69132t92">

(I will show how to get the Device ID in my next post.)

The result
And here is the result of our not so hard work.

AdMob in action

AdMob in action

You are now up and running with Google AdMob in your application! Now go make some money ;)