domenica 28 ottobre 2012

Windows Phone - Localization

When you publish a Windows Phone application in the Marketplace, you have more visibility if you support the region language.

An easy way to build a localized app is to use resources file (.resx).

First of all remember to set the default "neutral" language in the project properties! This is an important step because the "AppResources.resx" file is used from all not specified languages.

Add new file, and name it "AppResources.resx" for default language. For a new language, just add a new resource named with the CultureInfo ISO letters:
  • AppResources.it-IT.resx for italian language.
  • AppResources.cs-CZ.resx for czech language.
  • AppResources.fr-FR.resx for french language.
  • etc..
Remember to set all resource files to the public access modifier.



Now unload your project and edit the csproj file to add your supported language:
<SupportedCultures>it-IT;en-US;cs-CZ;fr-FR</SupportedCultures>

Create a class to get your resources.

public class LocalizedStrings
    {
        private static AppResources localizedResources = new AppResources();

        public AppResources Strings
        {
            get
            {
                return localizedResources;
            }
        }
    }

In your xaml you need to declare your class as resource:
<phone:PhoneApplicationPage.Resources>
     <local:LocalizedStrings x:Key="LocalizedStrings"/>
 </phone:PhoneApplicationPage.Resources>

Now you can use your localized string like this:
Text="{Binding Strings.Loading, Source={StaticResource LocalizedStrings}}"
Or from code behind:
txtLoading.Text = AppResources.Loading;

To test your app you can insert this code in application launching event:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("it-IT");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("it-IT");

All CultureInfo codes here.

venerdì 19 ottobre 2012

Windows Phone - Get Album Art

When you use MediaPlayer class in Windows Phone, usually you want to get the album cover for the current song.
You can access the current track with the ActiveSong property. This property is exposed by the Queue of MediaPlayer. Then you can check out the album art.

This code is all you need:

if (MediaPlayer.Queue.ActiveSong.Album.HasArt)
{
 BitmapImage bmp = new BitmapImage();
 bmp.SetSource(MediaPlayer.Queue.ActiveSong.Album.GetAlbumArt());
 imgAlbum.Source = bmp;
}

sabato 6 ottobre 2012

Equality Comparer with Lambda

Today, it's normal to write code with LINQ and Lambda Expressions.
But when you need to use IEqualityComparer, it's not possibile to use a lambda predicate.
The Interface needs a class, then you have to create a new type that inherit from IEqualityComparer.

You can use this workaround:
 
public class EqualityComparer<T> : IEqualityComparer<T>
{
   private Func<T, T, bool> _fnEquals;
   private Func<T, int> _fnGetHashCode;
   public EqualityComparer(Func<T, T, bool> fnEquals, Func<T, int> fnGetHashCode)
   {
      _fnEquals = fnEquals;
      _fnGetHashCode = fnGetHashCode;
   }
   public bool Equals(T x, T y)
   {
      return _fnEquals(x, y);
   }
   public int GetHashCode(T obj)
   {
      return _fnGetHashCode(obj);
   }
}

The simple usage is:

 
Dictionary<int, string> d = new Dictionary<int, string>() { { 1, "a" }, { 2, "a" }, { 3, "b" } };

var d2 = d.Distinct(new EqualityComparer<KeyValuePair<int, string>>((kvp1, kvp2) => kvp1.Value == kvp2.Value, kvp => kvp.Value.GetHashCode()));