Runar Ovesen Hjerpbakk

Programmer. Software Architect. Technical Manager.

Xamarin Forms

Compiled Xamarin Forms XAML - Specified cast is not valid

After releasing a new version of Golden Ratio Calculator, I decided to compile my XAML code to increase performance. I followed this guide from Xamarin and triggered compilation of all XAML in the assembly by adding the following to my App class.

Read More

Xamarin.Forms 24 hour TimePicker on iOS

I'm utilizing Xamarin.Forms in my next app to more easily support both iOS, Android and Windows Phone using the (mostly) same code base. It's relatively new however, and some use cases are not yet supported using Xamarin's supplied controls.

The Problem

My problem today was showing a TimePicker using a 24 hour time format, regardless of the users selected locale. The TimePicker normally respects the users preference, but I'm using the control to select a range of time, not a point in time.

TimePicker's Format property only affects the string showing the selected time, not the selection itself.

Forcing 24 hours on iOS

Since my next app will first be released on iOS (naturally), I tackled the problem there first. My solution was to create a custom renderer which forces the underlying UIDatePicker to always use a local with a 24 hour time format. In this case I chose Norwegian, but any 24h locale will do.

The picture illustrates what I wanted to accomplish. The picture illustrates what I wanted to accomplish.
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using PersonalTrainer.iOS.View.Controls;

[assembly: ExportRenderer (typeof (TimePicker), typeof (TimePicker24HRenderer))]
namespace YourNamespace.iOS.View.Controls {
  public class TimePicker24HRenderer : TimePickerRenderer {
    protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e) {
      var timePicker = (UIDatePicker)Control.InputView;
      timePicker.Locale = new NSLocale("no_nb");

To use the code yourself, just drop the class into your iOS project and use the TimePicker normally in your shared project or PCL.

Read More

Xamarin.Forms and iOS Simulator Scrolling

Here are a couple of Xamarin quick tips i have found useful the last couple of days.

iOS simulator refuses to scroll

If your iOS simulator refuses to scroll your ListView or TableView, try restarting it. The simulator sometimes locks up and disables scrolling for some reason. This has only happened during my Xamarin.Forms development, but I have no clue as of the source.

Open another instance of Xamarin Studio

To open another instance of Xamarin Studio on OS X, use the following command in the

open -n "/Applications/Xamarin"

To open another instance of Xamarin Studio on OS X, use the following command in the

open -n "/Applications/Xamarin"

Given that Xamarin Studio is in its default location. Sometimes Unix can be useful, who knew? 😬

Read More