MvvmCross, SyncFusion SfChart and UWP .NET Native tool chain compilation

I ran into an issue where my Xamarin.Forms UWP app, which uses MvvmCross and SyncFusion SfCharts stopped showing charts once I created the app package for the Windows Store (i.e. created *.appxupload package with .NET native tool chain).

I went hunting for a fix and rather quickly found a post on the SyncFusion official support forum about that problem. I also found that there’s an official Knowledge Base article:

Post: https://www.syncfusion.com/forums/127258/chart-not-rendered-in-uwp-release-mode

Knowledge Base: https://www.syncfusion.com/kb/7149/how-to-make-syncfusion-xamarin-forms-chart-to-work-in-uwp-in-release-mode-when-net-native-tool-chain

Both saying the same thing, that one should go to their App.xaml.cs file and add an extra assembly (i.e. typeof(Syncfusion.SfChart.XForms.UWP.SfChartRenderer).GetTypeInfo().Assembly)
to their Xamarin.Forms.Init() parameter in the OnLaunched() method. Basically add this code:

var assembliesToInclude = new List<Assembly>(); 
assembliesToInclude.Add(typeof(Syncfusion.SfChart.XForms.UWP.SfChartRenderer).GetTypeInfo().Assembly); 
Xamarin.Forms.Forms.Init(e , assembliesToInclude);

Unfortunately, this doesn’t work with MvvmCross 6.1.2 (probably doesn’t work with 6.2.0 as well – haven’t checked though). The charts are still not showing, even though the SfCharts assembly got included.

I can’t be 100% sure of the reason behind this problem but I suspect it’s because of the fact, that MvvmCross framework is now handling the Xamarin.Forms initialization for you. Meaning that the Xamarin.Forms.Init() method is called multiple times – once in the App.xaml.cs file and once inside of the framework. Another assumption, based on the results we're seeing, is that Xamarin.Forms.Init() works in a way that the last in is the one that gets served. In other words, if the Init() in App.xaml.cs happens before the one inside of the MvvmCross, then the SfChart assembly is not going to be included and Native tool chain compilation will strip it out.

A bit more explanations of the above: Mvx framework calls the Xamarin.Forms.Init() method inside of the base Setup file – MvxFormsWindowsSetup calling Xamarin.Forms.Init(). The remedy for the earlier described problem is simple, move the code from App.xaml.cs to the GetViewAssemblies() method override in your Setup.cs file. The GetViewAssemblies() method is being called at the launch of the app by MvvmCross and it is designed to retrieve additional assemblies to be passed as a parameter into the Xamarin.Forms.Init() method – which is exactly what we want. Below code example:

public class Setup : MvxFormsWindowsSetup<MvxApp, XamFormApp>
{
    public override IEnumerable<Assembly> GetViewAssemblies()
    {
        var customAssemblies = new List<Assembly>()
        {
            typeof(Syncfusion.SfChart.XForms.UWP.SfChartRenderer).GetTypeInfo().Assembly
        };
        customAssemblies.AddRange(base.GetViewAssemblies());
        return customAssemblies;
    }
}

That’s it – you should see your charts again!