HTML and Razor print with Xamarin for iOS


I recently had to add a print functionality on an iPad app, developed with Xamarin.

iOS provides a very nice feature, called UIMarkupTextPrintFormatter, which allows printing HTML formatted content, you can easily format the document you have to print.  That’s pretty cool!

The other amazing thing, Xamarin supports the Preprocessed Razor Template! We can also generate dynamic HTML with a data model. To do that, you have to:

In Xamarin Studio or Visual Studio:

Create a new Data Model / Entity: ExpenseReport.cs

public class ExpenseReport
    public string Item { get; set; }
    public double Price { get; set; }
    public DateTime Date { get; set; }


Add a new item: Preprocessed Razor Template, ExpenseReport.cshtml

  • Modifiy the cshtml file
  • Add the @model
  • Create your html
  • Add dynamic content
  • Save, cs files are going to be generated
@model IEnumerable<ExpenseReport>

    @foreach (var item in Model)
        <span style="font-weight:bold">@item.Item</span>
        <span>$ @item.Price</span>
        <span>@item.Date.ToString("MM-dd-YYYY HH:mm")</span>


Add Print Code

var printInfo = UIPrintInfo.PrintInfo;
printInfo.OutputType = UIPrintInfoOutputType.General;
printInfo.JobName = "Expense report";

var template = new ExpenseReport() { Model = myDataModel };
var page = template.GenerateString ();
var textFormatter = new UIMarkupTextPrintFormatter (page);
textFormatter.StartPage = 0;

var printer = UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintFormatter = textFormatter;
printer.ShowsPageRange = true;
printer.Present (true, (handler, completed, err) => {
	if (!completed && err != null) {
		Console.WriteLine ("error");


With that 2 features, we can very easily print dynamic HTML content. That great!

Xamarin.Forms for Windows Store app


Hi, I am a big fan of all Xamarin products and specially to Xamarin.Forms. Many people ask this, why there is no Xamarin Forms for Windows Store app !!! Yes, currently you can use Xamarin Forms for iPad and Android tablet, but not for Windows 8 tablet... ;(

That's why I have made an implementation of Xamarin.Forms for Windows Store app (called Xamarin.Forms.WinRT).

This is a first alpha release, you can find all the source code here in github:

The available controls are:

  • Button
  • Entry
  • Image
  • ListView (10%)
  • NavigationPage (with full navigation)
  • ContentPage
  • Slider
  • StackLayout
  • Switch
  • TabbedPage
This is a very first alpha, I will continue to improve the code regularly. if you are interrested, any helps will be appreciated :)

Techdays Lausanne - The slides

Hi all,


The techdays in Lausanne is now finnish. Thanks to Microsoft for the perfect organisation, good job guys !

The slide are right now available: 

2013 TechDays - LOB Windows Phone 8 v2.pptx (5,68 mb)

Techdays in Lausanne, see you there !



Hi Folks,


I'm proud to see you at Lausanne (ch) to speek about Windows Phone 8 and LOB app.


You can find the full agenda here:


See you :)

What is this movie ? My new Windows Phone application


I'm proud to annouce the availibility of my new Windows Phone application: What is this movie ?

What is this movie is the only game on Windows Phone that allows you to test your cinematographic knowledge!
The goal is simple, from a scene, find the name of the movie.
We have a database of over 3500 movies with over 55,000 photos.

Test your knowledge of the 7th Art!

Download now !

Windows Azure is in the light side of the force

Since many years, Windows Azure is a very powerful cloud solution.

I have now the prouve !! 

Windows Azure is in the light side of the force !!

Check this picture to know who built the datacenters :)


Repository Pattern with Windows Azure Mobile Services for Windows Phone app and Windows Store app


Windows Azure Mobile Services is a fantastic tool and very easy to use. In this post I will show you how to use the repository pattern with the Mobile Services SDK.

If you are familiar with Entity Framework, this pattern is used in many projects and samples. You can find a definition here or here

In this sample, we will use the portable library to share our data access layer with Windows Phone, Windows 8 and .net apps.

At the end, we will be able to inject our repository in our services / view models and to make our code testable.

At first, we will create 3 projects with the template Portable Class Library.
  • MyNamespace.Models
  • MyNamespace.Data
  • MyNamespace.Data.MobileService


This project contains our data model. It’s just a simple class with describes our entities:
public class ToDoItem
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }


This project contains the contracts (interfaces) for the repositories.
First, we will create the base interface, called IRepository. It contains all CRUD base operations.
public interface IRepository<T>
        Task<IEnumerable<T>> GetAllAsync();
        Task CreateAsync(T entity);
        Task UpdateAsync(T entity);
        Task RemoveAsync(T entity);
As explained, the repository pattern need a repository interface for each data model. Here the contract for the model ToDoItem:
public interface IToDoItemRepository : IRepository<ToDoItem>
It’s in the interface that we will add customs queries, if needed.


The project contains the implementation for Mobile Services of our interfaces.
First, you have to add the mobile services reference to you project with nuget. I recommend to use the latest prerelease version (
We will create a base repository with all base operations for the CRUD.
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
        protected IMobileServiceClient MobileServiceClient { get; set; }
        protected BaseRepository(IMobileServiceClient mobileServiceClient)
            if (mobileServiceClient == null) throw new ArgumentNullException("mobileServiceClient");
            this.MobileServiceClient = mobileServiceClient;
        protected virtual IMobileServiceTable<TEntity> GetTable()
            return this.MobileServiceClient.GetTable<TEntity>();
        public virtual Task<IEnumerable<TEntity>> GetAllAsync()
            return GetTable().ToEnumerableAsync();
        public virtual Task CreateAsync(TEntity entity)
            return GetTable().InsertAsync(entity);
        public virtual Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate)
            return GetTable().Where(predicate).ToEnumerableAsync();
        public virtual Task UpdateAsync(TEntity entity)
            return GetTable().UpdateAsync(entity);
        public virtual Task RemoveAsync(TEntity entity)
            return GetTable().DeleteAsync(entity);
Now, we are ready to write the implementation of IToDoItemRepository
public class ToDoItemRepository : BaseRepository<ToDoItem>, IToDoItemRepository
        public ToDoItemRepository(IMobileServiceClient mobileServiceClient) : base(mobileServiceClient)
This implementation is the base. If you have custom query, you can simply add you methods in the repository.

How to use my repository?

Our repository pattern is now implemented, we can easily use it in a Windows Phone project.
In my WP project, I really enjoy to use Caliburn.Micro and Autofac. You can add the nugget reference to Caliburn.Autofac. You can now register the repositories and the MobileServiceClient.
public class Bootstrapper : AutofacBootstrapper
        protected override void ConfigureContainer(ContainerBuilder builder)
            // register MobileServiceClient
                .WithParameter("applicationUrl", "MyAppUrl")
                .WithParameter("applicationKey", "MyAppKey")
            // register ToDoItemRepository
Your repositories are now registered, you can call them in the viewModels.
public class MainViewModel : Screen
        private readonly IToDoItemRepository _toDoItemRepository;
        private List<ToDoItem> _toDoItems;
        public MainViewModel(IToDoItemRepository toDoItemRepository)
            _toDoItemRepository = toDoItemRepository;
        protected override void OnActivate()
        private async void LoadData()
            // create an entry for the test
            await _toDoItemRepository.CreateAsync(new ToDoItem {Title = "my title", Description = "my description"});
            // load all data
            ToDoItems = new List<ToDoItem>(await _toDoItemRepository.GetAllAsync());
        public List<ToDoItem> ToDoItems
            get { return _toDoItems; }
                if (Equals(value, _toDoItems)) return;
                _toDoItems = value;
                NotifyOfPropertyChange(() => ToDoItems);
And voila, the repository pattern with Azure Mobile Services is now implemented. Thanks to the portable library, our data access layer is fully shareable with Win8 / .net apps.
In a next post, we will see how to create unit tests to test the view models.
Enjoy !

Source code: Mahalo.MobileServiceRepositoryPattern.7z (5,48 mb)

Autofac and Quartz.NET

Quartz.NET is an open source project for job scheduling and is a port of popular open source Java job scheduling framework.

If you usually used DI in your project, you should want to used it the job. To do that with Autofac, you have to create your own factory.

At first, create the class AutofacJobFactory:

public class AutofacJobFactory : IJobFactory
    private readonly IContainer _container;

    public AutofacJobFactory(IContainer container)
        if (container == null) throw new ArgumentNullException("container");
        _container = container;

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);

 And now, you can configure Quartz to use AutofacJobFactory to create their jobs.

string mySchedulerCronExpression  = ConfigurationManager.AppSettings["MySchedulerCronExpression"];
IContainer = container = InitializeDependencyInjectionContainer();
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler();
scheduler.JobFactory = new AutofacJobFactory(container);

IJobDetail job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
ITrigger trigger =

scheduler.ScheduleJob(job, trigger);

And voila :) Your job are now injected via Autofac !

Edinburgh Airport app for Windows Store

I'm currently working on a new app for Windows 8: Edinburgh Airport. This app is not official and I develop it because I often need to know some stuff like the flight infos...

My app is not yet available in the store but I hope to release it very soon.

I already make some screenshot.

MyNes for Windows 8

After a couple of month of working on my new project, I'm proud to show you the first video of MyNes, a NES emulator for Windows 8.