XAM’s Favourite (New) C# Features

At XAM Consulting (Xamarin Developers) our developers love to stay on top of the current trends in all aspects of software and mobile development. Last week we had a slack conversation geeking out about our favourite new C# features. I thought it would be nice to share some of the new features we love.

Majority of the features are C#7 and you can use them right now, in Xamarin and .NET.


My favourite c# feature is the default implementation for a interface. This is my favourite because it solves the problem of developers of duplicating code implementations for an interface while still getting the power of multiple inheritance of it.

public interface IDocumentTransfer
    void TransferObject();
    void ConnectToStorage() 

Jesse & Matthew Robbins

Oh yeah! My favourite C# 7 feature is `out var`s’ (Out variables)

public bool Validate(string input, out string message)
    // ….
var result = Validate("Hello World", out var failureReason);

I love it as it saves an unnecessary variable declaration. Instead you can just inline it

I also love the `if (type is SomeOtherType someOtherType)` syntax for checking typecasts

Matthew B

The nameof operator which we now use extensively. It’s subtle but powerful because helps rid your codebase of those string literals. Yay.

EmailPlaceholderColor = Consts.ErrorColor;


Tuples and Deconstructing

(string, bool) EnsureMaxLength(string input) =>
    input.Length <= 5 ? (input, false) : (input.Substring(0, 5), true);

// Don't care if the value was trimmed or not, using "discard" variable name (underscore symbol):
var (processedVal, _) = EnsureMaxLength("abcd");

// Can check whether the value was trimmed by inspecting wasTrimmed variable:
var (processedVal2, wasTrimmed) = EnsureMaxLength("abcdef");


The inline getter arrow (“expression-bodied members”).

public bool IsOptionActive { get; set; }
public ImageSource OptionImage => IsOptionActive ? “Option_Enabled” : “Option_Disabled”; (edited)

Myself (Michael)

Personally I love the new ability to assign variables using the is statement with pattern matching.

private static void PrintObject(dynamic x)  
    if (x is Customer c)  
        WriteLine($"Customer Id : {c.Id} Customer Name {c.Name}");  

In my years I’ve met many developers who don’t keep up with the new releases of languages, this is a little unfortunate for them because these new features help make code cleaner, more concise and increases readability.

This content originally appeared on the XAM Consulting – Blog.

Beautiful Xamarin – Facebook Clone in Xamarin.Forms

I’ve actually had this project around since this start of the year but I never blogged it. I did this code for my speaking session at Microsoft Ignite in Australia.

Warning – This code is a playground there’s lots of play code and not reflective of a production application. This project is for only used for learning about Xamarin.Forms features.

In this blog post I’ll go over some of the features want to demo with this app, with some screenshots. Over the coming weeks I plan to demonstrate that anyone who says you cannot build fast and beautiful apps in Xamarin.Forms is simply wrong, lying or ignorant. The deal is that you just need to know how Xamarin.Forms work.

Post-Layout Animations/Translations – Right Slide Bar

One of the most powerful features of Xamarin.Forms are the Post-Layout translations. fbforms-sidebar

People are so surprised when I tell them this is done without any custom renders, let’s take a look at how.

In this case I make use of the Grid in Xamarin.Forms, in actual fact the sidebar is layered over the listview which in the background. The Grid only has a single column.

  <FriendList ... (300 Set Width)
  <Header ...(Full Screen Width)
  <ListView ...(Full Screen Width)

So the FriendList normally sits on top of the List but what we actually do is translate the X coordinate so it sits off the screen, just after the page size allocated.

protected override void OnSizeAllocated(double width, double height)
    base.OnSizeAllocated(width, height);

    friendList.TranslationX = this.Width;

Then the awesomely simple part, when a user taps the menu button we just translate the views.

void FriendListTapped(object sender, System.EventArgs e)
    if (friendList.TranslationX == Width) // determine if open
        friendList.TranslateTo(Width - 300, 0, 100); //open the friendlist
        myListView.TranslateTo(-300, myListView.TranslationY, 100); //move the listview to allow for friendlist
        header.TranslateTo(-300, header.TranslationY, 100); //move the header to allow for friendlist
        friendList.TranslateTo(Width, 0); //close the friendlist
        header.TranslateTo(0, header.TranslationY, 100); //put the header back
        myListView.TranslateTo(0, myListView.TranslationY, 100);  //put the listview back

Disappearing NavigationBar

This one was a little harder and it’s still not working perfectly.


In this case our grid has 2 rows, 80 at the top for the header size and a * for the rest.

    <RowDefinition Height="80"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>

On the default state the header just sits in the first row.

<Grid x:Name="header" BackgroundColor="{StaticResource BlueHeaderColor}" >

While the ListView actually uses the Grid.RowSpan=”2″ so it goes over multiple rows, I had to do this because the height of the listview needs to be the full height of the screen once the header disappears. I also make use of the ability to set post-layout values in Xaml, setting the TranslationY to 60 on the ListView (this moves it down into the correct position by default).

<local:AdvancedListView BackgroundColor="#D7DBDF" HasUnevenRows="true" TranslationY="60" Grid.RowSpan="2" 			
		ItemsSource="{Binding Items}" ItemTemplate="{StaticResource homeWallListTemplateSelector}" 

So when we want to hide the navigation bar we must hook into the Scrolled event on the listview and translate the Y values on the views depending on the scroll position. This part is really just for a demo, it’s not thought out and very prototype.

void MyListView_Scrolled(Point obj)
    var headerHeight = 60;

    if (obj.Y <= 0)
        header.TranslationY = 0;
        myListView.TranslationY = headerHeight;
    else if (obj.Y <= headerHeight)
        header.TranslationY = -obj.Y;
        myListView.TranslationY = -obj.Y + headerHeight;
    else if (obj.Y > headerHeight) //scrolled 
        header.TranslationY = -headerHeight;
        myListView.TranslationY = 0;

Like Animations

One other thing I like about this app is the animated likes buttons.


In this case I cheated a little bit and used a Animated Gif, it worked ok. I also used a few other features.

As with all the other cases I leveraged the Grid. You’ll also notice that I used a few extra controls, AdvancedFrame from FreshEssentials to allow for the rounded edges and a GifImageViewControl.

<ContentView Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="4" x:Name="likeArea"  Opacity="0">
    <fe:AdvancedFrame Padding="15, 4" HasShadow="true" OutlineColor="#EEEEEE" CornerRadius="70" Corners="all"  InnerBackground="White"  >
        <local:GifImageViewControl  Aspect="AspectFill" Source="facebookreactionsanimation.gif"></local:GifImageViewControl>

As with all the other samples we simply leverage some Translations and in this case we also leveraged the FadeTo animation.

async void Handle_Clicked(object sender, System.EventArgs e)
    if (likeArea.TranslationY == -35)
        await likeArea.TranslateTo(0, 0, 100, Easing.Linear);
        await Task.Delay(50);
        await likeArea.FadeTo(0, 50);
        await likeArea.FadeTo(1d, 20);
        await likeArea.TranslateTo(0, -35, 100, Easing.Linear);

If you want to check out the code base please find it here: https://github.com/XAM-Consulting/FacebookForms



Why developers should care about CX!

Normally I like to write about deep technical topics but CX (Customer Experience) is another one of my passions. I’m passionate about CX because I’ve had so many bad experiences with big brands and this trend is a welcome change.

I recently wrote an article over on linkedin about ‘Why you should care about CX’.

I think as developers we need to be across these trends, it’s often that these types of trends are what drives our work and it’s nice to know that we can help people have a better experience.

Please take a look at the post and let me know what you think: https://www.linkedin.com/pulse/why-you-should-care-cx-michael-ridland



The Definition of Done (DoD) for Xamarin Developers

A DoD or Definition of Done is a software development term, the term is common in many agile teams and it has origins in SCRUM. The basic idea is that a team has a shared understanding of what defines a task as Done. What a project manager might foresee as ‘Done’ can be different to a developer or a customer. At XAM Consulting we deliver high quality solutions for our customers and therefore a solid DoD is essential. It’s important all developers understand that just because they’ve made a code change this does not mean it’s done.

We’ve recently revised our Definition of Done and thought we would share it because it’s a good marker on what it means to be ‘Done’ as a Xamarin developer.

Note* This is only a checklist for what’s done as a developer, it’s not the complete software lifecycle, in order for this to be successful we also need to follow modern software engineering techniques including gitflow, CI and TestCloud Testing.

So here I present… The Definition of Done (DoD) for Xamarin developers. (This DoD could also apply for Mobile Developers or Cross Platform developers)

Code respects .NET Coding Guidelines
We follow .NET coding guidelines outlined here – https://github.com/dennisdoomen/CSharpGuidelines

Code respects XAM Consulting’s Code Quality and Architecture Guidelines
We’ve not published this in the public domain (yet) but in summary we follow industry best practices for code quality. Our code bases are designed to be maintainable in the long term. Here’s a short summary:

  • Code is Loosely Coupled
  • Code has High Cohesion
  • Code makes use of OOP (avoiding pitfalls with inheritance, prefer composition over inheritance)
  • Interface Driven Development – programming to small interfaces
  • Classes are small
  • Methods are small
  • Code follows SOLID Principles
  • Use and understand design patterns
  • Make use of Reactive patterns

Tested on all target platforms
As our solutions are cross platform it’s essential that all code modifications are tested on all target platforms. If you test on a single platform it’s not done.

Tested on variety of screen sizes
It’s easy to code a UI for a single screen size but much harder to have it work on multiple sizes, especially smaller devices. All code modifications must be tested on different screen sizes.

Tested on a variety of physical devices
Real devices behave differently to simulators in many situations. All code modifications must be tested on a variety of devices, internally at XAM Consulting we have a good variety of different physical devices for internal testing before we go into TestCloud.

All possible side effects tested in system
If you make modification in a complex system it’s essential that you test for any possible side effects.

Tested in Release Mode
Applications can behave much different in Debug mode then Release. We need to make sure we’re testing the application in a release mode.

Application must handle intermittent connections
Mobile devices have transient connections, there needs to be a strategy for handling these types of connections.

Units Tests
Unit tests are an essential component of a high quality codebase. If it doesn’t have unit tests it’s not done.

UITests developed covering features
Xamarin TestCloud is a great tool, it’s saved us (from production issues) a number of time. We now have UITests and CI with (TestCloud) on all of our projects.

Peer reviewed code
This is a no brainer, peer reviewed code is essential. We made this a part of our lifecycle using gitflow.

Peer reviewed for end-user acceptance
It’s important that not only the code is reviewed we also need to ensure that the problem is well understood. It’s important that two or more people discuss and understand the issue. Once understood the developers must both test the solution will meet the user acceptance criteria.

Issues must be reproduced
If you don’t reproduce an issue it cannot be confirmed as solved. The issue that is reproduced must be exactly the same as the issue that’s reported, otherwise there might be two issues.


Essentially, the goal is to have no issues when shipped into a UAT environment. If you ship a issue into UAT you need to review why that issues went into UAT.

At XAM Consulting it’s our commitment to ship world class apps and I hope that you can also make the effort.


Xamarin Plugins / .NET Standard with Martijn van Dijk and Michael Ridland

In this video I chat with Martijn van Dijk about Xamarin Plugins and a little bit of .net standard. It was filmed at the Xamarin Dev Days in Singapore 2016, sorry it’s taken a while to upload.

Martijn is doing some great work in the Xamarin community and you can follow him on twitter and github.

Links from the interview:

Plugin For Xamarin Templates


Hacking the Xamarin.Forms Layout System for Fun and Profit

If you’re a Xamarin.Forms developer, it’s likely you already know about the two great talks that Jason Smith did at Evolve this year (2016). Both of the talks were on performance in Xamarin.Forms and they were outstanding because we got a truckload of performance tips that we’ve never had previously. If you’ve watched them once we would recommend you also consider watching them again, there’s alot of content and you’ll learn something new. This insight into the Xamarin.Forms performance and the layout system has been a key part of us building performant apps in Xamarin.Forms at XAM Consulting.

But we wanted more, we wanted more speed and more performance, we want native performance. The tips we got were great, but we wanted to understand why these tips existed because if we understand the system we can use that understanding to develop our own tips and techniques for Xamarin.Forms performance.

One of the points that Jason makes is about the Xamarin.Forms Grid, the suggestion is that we should avoid using the ‘Auto’ setting for columns and rows. Why is this? Well it turns out that if a view inside a Auto column changes size (or generally does anything to invalid measure) the grid will be doing a full layout of all children, well not only the grid but the Grid’s parent and parent after that.

If you watch both of the talks, it’s clear the root of performance in Xamarin.Forms is in the layout system, so let’s look into this Layout System. Now that Xamarin.Forms is open source we can use the code a understand the layout system.

The layout system is broken up into two parts/cycles, 1) the invalidation cycle 2) the layout cycle.


Invalidation Cycle

To understand the invalidation cycle let’s take a look at the Xamarin.Forms layout code.

As we can see in this code below each VisualElement.cs has a event MeasureInvalidated, VisualElement is the base class of Layouts, Views and Pages.


Then each time a child is added to a Page or Layout, the parent attaches the MeasureInvalidated event of the child.


As we can see below the OnChildMeasureInvalidated event handler (for the children) then calls the MeasureInvalidated event, because the parent of that View is subscribed to the MeasureInvalidated even the parents OnChildMeasureInvalidated method is called. It’s important to note there’s conditional logic involved, the event will not always be called.

…Conditional Logic…

So here’s an example of what it looks like, with the events.


Here’s what happens when a child view becomes invalidated. As you can see the events bubble to the top.


That’s the invalidation cycle.

Layout Cycle

Let’s take a look at the layout cycle, the layout cycle happens in two cases:
1) When a layout is done for the first time, eg. when a page is first displayed.
2) After the invalidation cycle

As you can see in the image below, during the first part of the layout cycle measure is called on the child. It’s important to note that for the most part measure is called on all children and in many cases it’s called multiple times. After the children have been measured then layout is called on all children.


So here’s how the full layout looks.



You might be thinking why do I care about this? Well because we can hack it. It’s possible to short circuit the invalidation cycle at an early stage, as per the image below.


Let’s take a look at how we can test this. In order to test this I’ve created a page with a few labels and a single label that’s, updated on a timer 20 times. I’ve also linked up my Xamarin.Forms app into the Forms code base and added performance metrics to the methods inside Forms.


Let’s first take a look at the results of a StackLayout as per below. stacklayout

The results show that LayoutChildIntoBoundingRegion is called 243 times, every time the text of the label is changed a full layout cycle happens.


Let’s try the same with a Grid, as per below.


The results show that LayoutChildIntoBoundingRegion is called 6 times, it’s only called the first time the view is shown.


Let’s try the same Grid but using a ‘Auto’ for a row.


The result is back up to 243 times and the full layout is happening every time the label text is changed.


Ok, now let’s put back the Grid to using stars but I also want to try something different. I want to have children of the grid dynamically change size, so let’s test with a BoxView that changes width.


NB – In order for me to change the Width of the Box I need to change the HorizontalOptions on the BoxView from Fill to Start.


Even though we’ve changed the Height back to a star the result is still 243 and the full layout is happening everytime the label text is changed. It seems that when we change the HorizontalOptions of a child we also change change the invalidation cycle behaviour.


Ok, so I changed the BoxView back to Fill the BoxView will not change dynamically. Let’s try something different, lets put the BoxView inside a ContentView.


It’s back down again, great so we still get our children changing dynamically and we also short circuit the layout system.



  • A child of a stacklayout will always cause a full layout cycle, there’s no way a StackLayout can short circuit this cycle.
  • A child of a Grid with that’s inside rows and columns with static or star widths AND the LayoutOptions are set to Fill, are fully constrained which means that the invalidation cycle will be stopped at that child view.
  • In order to have children of a Grid change layout dynamically they need to be a child of another View which is fully constrained. (As per the Grid sample).

After taking a look at the Xamarin.Forms code base I can see this is the line that does the short circuit. It seems that a view.Constraint needs to be the value of LayoutConstraint.Fixed for this short circuit to occur. I did further research into this and found the only time we can short circuit the invalidation cycle is in the cases proven above.

UPDATE – Originally in this blog post I mentioned that the above cases are the only way to short circuit the invalidation cycle, this is not entirely true as the AbsoluteLayout can be used to short circuit the invalidation cycle. I will put together some more research for this for another post. 



Talk with Rui Marinho XLabs Founder and Xamarin.Forms Developer

Last weekend I was lucky enough to attend and speak at the Xamarin Dev Days in Singapore. I think Dev Days Singapore is the biggest Dev Days so far with 400 registrations and 250 that attended. It was even more awesome for myself as I got to catch up with some friends in the Xamarin world including Rui Marinho.

In this video I talk with Rui Marinho the founder of XLabs and now a developer on the Xamarin.Forms team. We delve into some important topics, we discuss how he got into Xamarin, announce that XLabs is Dead, talk about open source and a little about the future of Xamarin.Forms.


Be more awesome with MFractor for Xamarin Studio

In this video I interview Matthew Robbins the creator of MFractor a Xamarin Studio Plugin. If you’re not using MFractor your really missing out, it’s got some beautiful features which save you truckloads of time when building apps with Xamarin Studio.

If you’ve ever wanted a ReSharper for Xamarin Studio this is it. It’s got static analysis, code generation and advanced navigation features. It’s here right now and will only get more awesome into the future.

You can find out more about MFractor here: www.fractor.com and find Matthew on twitter.com/matthewrdev


Announcing FreshMvvm 2.1 and 2.2

Your favourite Mvvm Framework for Xamarin.Forms just keeps getting better. At XAM Consulting we use FreshMvvm in all of our serious production applications, the fact that we use FreshMvvm so intensely is the reason that FreshMvvm is easy to use, robust and flexible.

There’s been two releases of FreshMvvm since I last updated, Version 2.1 and Version 2.2.

Release 2.2.0

Better debug messages displayed

In previous versions of FreshMvvm construction errors of pages were in a few levels of inner exceptions, so if your xaml had a little error you had to dig into the inner exceptions. It was not an issue for experienced users of FreshMvvm but for people new to the Framework it was a bit hard to figure out the actual issue. Now exceptions will be bubbled up developers won’t need to look into inner exceptions.

Ability to remove a page from Navigation

In some cases you might want to remove a page from the Navigation stack. This works in the case you’ve Pushed a few pages but want to remove one of the pages from the stack before you return,  for example you’ve pushed to Page1 -> Page2 -> Page3, but you essentially you don’t want Page2 to be part of the stack anymore. In this case you can call the remove method.


Add ability to SwitchSelectedRoot page in Tabs and MasterDetail

This is another special finite feature of FreshMvvm, in which you can switch the currently selected page of the main tabbed page or the master detail page.

//switch the selected tab to the HomePageModel

//switch selected master to the HomePageModel

//switch selected to the HomePageModel


Release 2.1.0

Adds ability to control PageModel mapping via PageModelMapper

You now have the ability to control the convention of mapping ViewModel to pages. You do this by using the IFreshPageModelMapper.

public interface IFreshPageModelMapper
    string GetPageTypeName(Type pageModelType);

Currently the PageModel mapper is defaulted to  the FreshPageModelMapper.

public static class FreshPageModelResolver
    public static IFreshPageModelMapper PageModelMapper { get; set; } = new FreshPageModelMapper();

The current behaviour is to map PageModel or ViewModel to Page.

public class FreshPageModelMapper : IFreshPageModelMapper
    public string GetPageTypeName(Type pageModelType)
        return pageModelType.AssemblyQualifiedName
            .Replace ("PageModel", "Page")
            .Replace ("ViewModel", "Page");

It’s easy to implement your own custom mapper and then set the PageModelMapper.

FreshPageModelResolver.PageModelMapper = MyPageModelMapper();

Adds ability to navigate without animation

You can now push and pop PageModel without animations.

Better cleanup of objects

We clean up after ourselves, even better now. 🙂

IOC now has unregister feature

You can now unregister a class out of the IOC container.

Add First Only Tabbed Navigation Container

FreshMvvm has always had a built in tabbed navigation container, the behaviour of this navigation is to keep the tabs always present but this is not what you always want. In some case you want the tabs to disappear when you push to a page, so we created the FreshFONavigationContainer. The FO stands for First Only and it means this tab page will only appear on the first page of the app.

FreshMvvm in Production

It makes us happy to see that so many others are also delivering great apps using FreshMvvm. I was recently informed that the Fraedom Expense app was built with FreshMvvm. Great work.

As always the latest version of FreshMvvm is up in nuget right now.



Announcing FreshMvvm 2.0

Today we’ve published up to nuget FreshMvvm 2.0 with some very anticipated new features.

IOC Container Lifetime Registration Options

We now support a fluent API for setting the object lifetime of object inside the IOC Container.

// By default we register concrete types as 
// multi-instance, and interfaces as singletons
FreshIOC.Container.Register<MyConcreteType>(); // Multi-instance
FreshIOC.Container.Register<IMyInterface, MyConcreteType>(); // Singleton 

// Fluent API allows us to change that behaviour
FreshIOC.Container.Register<MyConcreteType>().AsSingleton(); // Singleton
FreshIOC.Container.Register<IMyInterface, MyConcreteType>().AsMultiInstance(); // Multi-instance

As you can see below the IFreshIOC interface methods return the IRegisterOptions interface.

public interface IFreshIOC
    object Resolve(Type resolveType);
    IRegisterOptions Register<RegisterType>(RegisterType instance) where RegisterType : class;
    IRegisterOptions Register<RegisterType>(RegisterType instance, string name) where RegisterType : class;
    ResolveType Resolve<ResolveType>() where ResolveType : class;
    ResolveType Resolve<ResolveType>(string name) where ResolveType : class;
    IRegisterOptions Register<RegisterType, RegisterImplementation> ()
        where RegisterType : class
        where RegisterImplementation : class, RegisterType;

The interface that’s returned from the register methods is IRegisterOptions.

public interface IRegisterOptions
    IRegisterOptions AsSingleton();
    IRegisterOptions AsMultiInstance();
    IRegisterOptions WithWeakReference();
    IRegisterOptions WithStrongReference();
    IRegisterOptions UsingConstructor<RegisterType>(Expression<Func<RegisterType>> constructor);

Switching out NavigationStacks on the Xamarin.Forms MainPage

There’s some cases in Xamarin.Forms you might want to run multiple navigation stacks. A good example of this is when you have a navigation stack for the authentication and a stack for the primary area of your application.

To begin with we can setup some names for our navigation containers.

public class NavigationContainerNames
    public const string AuthenticationContainer = "AuthenticationContainer";
    public const string MainContainer = "MainContainer";

Then we can create our two navigation containers and assign to the MainPage.

var loginPage = FreshMvvm.FreshPageModelResolver.ResolvePageModel<LoginViewModel>();
var loginContainer = new FreshNavigationContainer(loginPage, NavigationContainerNames.AuthenticationContainer);

var myPitchListViewContainer = new MainTabbedPage(NavigationContainerNames.MainContainer);

MainPage = loginContainer;

Once we’ve set this up we can now switch out our navigation containers.


New public methods

CurrentNavigationServiceName and PreviousNavigationServiceName are both public methods, so you can access them if you need to.

Some features (recently released)

Multiple Navigation Services
Custom IOC Containers
Pushing different views
Clean up after page is Popped


As always this is available on nuget now!

If you have any questions please ask on forums.xamarin.com and send me a email.

You can also find some more docs on the github repo.