When you are developing a web site and want to use a custom font, via font-face, you might find that IE on Windows Phone refuses to use it. Turns out that IIS doesn’t include the mime-type by default so you need to add it. If you don’t have access to the site directly (maybe Azure web site or a shared server) you can add it in your site’s web.config webserver section;

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
    </staticContent>
</system.webServer>
Advertisements

1080p 6 inch Silverlight vs. Jupiter

Anyone know how to resolve this? Given the simple XAML;

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>

        <Border Background="Red"  Grid.Row="0" >
            <TextBlock Text="Silverlight"  />
        </Border>

        <Rectangle Fill="Blue" Grid.Row="1" />
        <Rectangle Fill="Green" Grid.Row="2" />

    </Grid>

This results in some very different results;

Silverlight Jupiter
image image

The Silverlight version scales nicely with the phone, whereas the Jupiter version renders the text without scaling it. How do you resolve this?

HttpClient redirect gotcha

Just a quick post as I’ve just fallen foul of this.

Recently I changed by web-client to use the nice await/async HttpClient, e.g.

   
  HttpClient client = new HttpClient();   
  string result = await webClient.GetStringAsync(serviceRequest); 

The problem is that my service decided to be a good web citizen and change their API by sending a redirect. Unfortunatley the above code simple fell over…legs in the air.

The trick is to configure the HttpClient to allow redirects;

   
  HttpClientHandler handler = new HttpClientHandler();   
  handler.AllowAutoRedirect = true;   
  HttpClient webClient = new HttpClient(handler);            
  string result = await webClient.GetStringAsync(serviceRequest); 

Testing localisation on Windows Phone

Localising (or localizing) a Windows Phone application is straightforward, however, testing that you have correctly localised the text is harder. One method is to use a pseudo language but I found that has a couple of problems; you need to overwrite a locale (although you can use my previous posts to create a new ‘unsupported’ language), you need to maintain that language with your default language, and…I just find it over noisy. So I present an alternative.

 

Here is an example of an application I am working on;

image

If I switch on the ‘test localisation’ then the page looks like;

image

Argh, I’ve not localised ‘save’! The idea is that all the localised strings show up as their resource name surrounded by a ‘%’. I think this makes it easier to spot the non-localised text, plus you could turn it on during a test session therefore allowing you to report the resource id that is incorrectly localised for a specific language.

The code is based on a previous post but I’ll show the code again with the slight adjustments;

Note, if you want to dynamically support switching between a language and testing it, then you need to change the GetString to call the base class

public class TestResourceManager : ResourceManager
{
    public TestResourceManager()
        : base("MyApp.Localisation.StringLibrary", typeof(StringLibrary).Assembly)
    {
    }

    public override string GetString(string name, System.Globalization.CultureInfo culture)
    {
        return "%" + name + "%";
    }
}

Then add a constructor to the default string library.

    public class LocalisedStrings
    {
        private static readonly StringLibrary _localizedResources = new StringLibrary();

#if DEBUG_LOCALE
        static LocalisedStrings()
       {
           Type type = _localizedResources.GetType();
           FieldInfo info = type.GetField("resourceMan", BindingFlags.NonPublic | BindingFlags.Static);
           info.SetValue(null, new TestResourceManager());
       }
#endif

        public StringLibrary LocalizedResources { get { return _localizedResources; } }
    }

Automatic text hyphenation for Windows Phone

Recently the topic of text hyphenation for Windows Phone has cropped up. So I thought I’d give it a quick go. Note, this is an initial stab at a solution, I’m not suggesting it’s fully functional but certainly could be built upon.

The problem

image

 <TextBox> 
      FontSize="40"                     
      Width="170"
      TextWrapping="Wrap"
      Text="Hello world combinations can be extremely worrying" />                

Possible Solution

The design is pretty simple (although is it Right-To-Left compatible?), check when the text moves from one line to the next. When it does, check to see if the last character is part of a word. It it is, the add a hyphen.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Pauliom.Behaviors
{
    public class HyphenationBehavior : Behavior<TextBox>
    {
        private int lastLength = 0;
        protected override void OnAttached()
        {

            var textBox = this.AssociatedObject;
            textBox.TextChanged += textBox_TextChanged;
            textBox.Loaded += textBox_Loaded;
            base.OnAttached();
        }

        void textBox_Loaded(object sender, RoutedEventArgs e)
        {
            Hyphenate(sender);
        }

        void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Hyphenate(sender);
        }

        private void Hyphenate(object sender)
        {
            TextBox textBox = sender as TextBox;
            if (this.lastLength != textBox.Text.Length)
            {
                this.lastLength = textBox.Text.Length;
                Rect lastRectange = textBox.GetRectFromCharacterIndex(1);
                int x = 0;
                while (x < textBox.Text.Length)
                {
                    Rect r = textBox.GetRectFromCharacterIndex(x);
                    if (lastRectange.Top != r.Top)
                    {
                        lastRectange = r;
                        char endChar = textBox.Text[x - 1];
                        if (endChar != ' ' && endChar != '-')
                        {
                            textBox.Text = textBox.Text.Substring(0, x - 1) + "-" + textBox.Text.Substring(x - 1);
                        }
                    }                    
                    x++;
                }
            }
        }

        protected override void OnDetaching()
        {
            var textBox = this.AssociatedObject;
            textBox.TextChanged -= textBox_TextChanged;
            base.OnDetaching();
        }
    }
}

image

 <TextBox> 
      FontSize="40"                     
      Width="170"
      TextWrapping="Wrap"
      Text="Hello world combinations can be extremely worrying">    
      <i:Interaction.Behaviors>
         <pauliom:HyphenationBehavior />
      </i:Interaction.Behaviors>                
 </TextBox>            

As I said, it probably is not production ready code but could be built upon.

Microsoft Visual Studio XAML UI Designer – kill, kill, kill

I’ve blogged before that in solutions that share xaml between DLLs the XAML UI Designer can cause problems by locking files and blocking builds. The fastest solution is usually to kill off the designer process. Here is a bit of powershell to do just that;

Get-Process -Name XDesProc | Stop-Process

Workaround for ListPicker missing writable ItemCountThreshold

The ListPicker in Wp7 Toolkit allowed you to specify how many items could be shown before it went into full-screen mode. Quite correctly that has been removed and is now hardcoded to 5. Well, rules are there to be broken, and if you *really* need to workaround the problem then you can try the following (it works at the moment, may not work in the future)

Type type = MyListPicker.GetType();
PropertyInfo itemCountThreshold = type.GetProperty("ItemCountThreshold");
itemCountThreshold.SetValue(MyListPicker, 20);

Looking at the reflection code I released that I hadn’t actually done anything sneaky. So I replaced it with the following…more graceful code;

MyListPicker.SetValue(Microsoft.Phone.Controls.ListPicker.ItemCountThresholdProperty, 20);