Default constructor not found for type Esri.ArcGISRuntime.Xamarin.Forms.MapView

2239
4
Jump to solution
11-28-2016 11:50 AM
SchuylerDuarte
New Contributor II

Hi,

Using Xamarin Studio on Mac OS X, I created a brand new Xamarin forms project and added the Esri.ArcGISRuntime.Xamarin.Forms and Esri.ArcGISRuntime.Xamarin.iOS packages (both v100.0.0) via NuGet.  Created a simple Xaml page just like the sample:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
xmlns:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime" 
xmlns:local="clr-namespace:EsriTest" 
x:Class="EsriTest.EsriTestPage">
    <Grid>
        <esriUI:MapView x:Name="MyMapView"/>
      </Grid>
</ContentPage>

With the code behind:

public partial class EsriTestPage : ContentPage
    {
        public EsriTestPage ()
        {
            InitializeComponent ();
            
            Initialize ();
        }
        
         private void Initialize ()
        {
            // Create new Map with basemap
            Map myMap = new Map(Basemap.CreateImagery());

            // Assign the map to the MapView
            MyMapView.Map = myMap;
        }
    }

It compiles and deploys fine to an iPad Air device (iOS 10.1.1), but when I hit this page I get a System.MissingMethodException - Default constructor not found for type Esri.ArcGISRuntime.Xamarin.Forms.MapView:

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            this.LoadFromXaml(typeof(EsriTestPage)); // throws exception here
            MyMapView = this.FindByName <global::Esri.ArcGISRuntime.Xamarin.Forms.MapView>("MyMapView");
        }

Any help is appreciated... thanks.

0 Kudos
1 Solution

Accepted Solutions
TonyWakim
Esri Contributor

Schuyler,

Try changing the following setting in the IOS project options (if not set already):

Set the Linker behavior on project options --> iOS Build to "Don't link"

Let me know if this resolves the error you are getting.

Tony Wakim

View solution in original post

0 Kudos
4 Replies
TonyWakim
Esri Contributor

Schuyler,

Try changing the following setting in the IOS project options (if not set already):

Set the Linker behavior on project options --> iOS Build to "Don't link"

Let me know if this resolves the error you are getting.

Tony Wakim

0 Kudos
SchuylerDuarte
New Contributor II

Looks like that did the trick for iPhone/iPad.  Thank you much!

Schuyler

0 Kudos
RichZwaap
Occasional Contributor III

Hi Schuyler,

I wanted to clear up what's going on here and what the available options for addressing it are.  First, what the linker does is analyze the code in your app and discard anything that's not used - more info here.  By default, it does not include XAML in what it looks at.  In your case, the MapView class itself is only being referenced in XAML and not anywhere in your code.  So the linker then thinks that nothing in the Esri.ArcGISRuntime.Xamarin.Forms assembly (which essentially just includes the Xamarin Forms MapView and SceneView) is being used, so it excludes this assembly from the final compiled app.

Setting the linker to "don't link" gets around the issue by opting out of linking entirely.  This is fine for debug builds, but for release you wouldn't want to do this, as it opts out of the size reductions the linker can bring.

Alternatively, there are two ways to get around this while still letting the linker do it's thing:

  1. Opt-in to XAML Compilation.  This will enable analysis of the XAML at compile-time, so the linker will then recognize that the MapView class is in fact used and thus preserve the Esri.ArcGISRuntime.Xamarin.Forms assembly.  To do this, add "[assembly: XamlCompilation(XamlCompilationOptions.Compile)]" to the AssemblyInfo.cs file in your iOS app project.
  2. In your code behind, add a method that never gets called that includes a reference to a class in the Esri.ArcGISRuntime.Xamarin.Forms assembly. Mark the method with the [Preserve] attribute to instruct the linker not to remove it.  You could, for instance, add this:

    // this code is never executed, but is included to prevent the linker from excluding the Esri.ArcGISRuntime.Xamarin.Forms assembly from the compiled app
    [Xamarin.Forms.Internals.Preserve]
    private void UnusedMethod()
    {
        var unusedMapView = new Esri.ArcGISRuntime.Xamarin.Forms.MapView();
    }

Hope this helps.

-Rich

SchuylerDuarte
New Contributor II

Awesome Rich.  Thank you for the additional information and explanation.

Schuyler

0 Kudos