Apple Geek
Apple Geek

Reputation: 1121

System.TypeInizilationException has been thrown

I'm using xamarin forms, and it's been working fine but suddenly my app is crashing on startup with the error: System.TypeInizilationException has been thrown, and it show my App.Xaml.cs class:

using Xamarin.Forms;

namespace MyApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new AppPage());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

It highlights the line MainPage = new NavigationPage(new AppPage()); as the source of the issue, what would be causing the problem and how do I fix it?

Here is the code of the AppPage class:

using Xamarin.Forms;
using Microsoft.WindowsAzure.MobileServices;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApp
{
public partial class AppPage : ContentPage
    {

        public AppPage()
        {
            InitializeComponent();
        }


        protected override async void OnAppearing()
        {

            base.OnAppearing();

            //Code

        }




    }

}

App.Xaml class:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.app">
    <Application.Resources>
        <!-- Application resource dictionary -->



    </Application.Resources>
</Application>

Inner Exception:

System.PlatformNotSupportedException: The empty PCL implementation for Microsoft Azure Mobile Services was loaded. Ensure you have added nuget package to each of your platform projects.
  at Microsoft.WindowsAzure.MobileServices.Platform.get_Instance () [0x00007] in <7910de25d05d49d9b3c2d648cd285e40>:0 
  at Microsoft.WindowsAzure.MobileServices.MobileServiceClient.GetApplicationInstallationId () [0x00004] in <7910de25d05d49d9b3c2d648cd285e40>:0 
  at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor (System.Uri mobileAppUri, System.Net.Http.HttpMessageHandler[] handlers) [0x0005f] in <7910de25d05d49d9b3c2d648cd285e40>:0 
  at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor (System.String mobileAppUri, System.Net.Http.HttpMessageHandler[] handlers) [0x00008] in <7910de25d05d49d9b3c2d648cd285e40>:0 
  at ChurchBuilder.ChurchBuilderPage..cctor () [0x00000] in /Users/taylordowns/Projects/ChurchBuilder/ChurchBuilder/ChurchBuilderPage.xaml.cs:24

Upvotes: 0

Views: 66

Answers (1)

TheGeneral
TheGeneral

Reputation: 81483

For these types of benign problems and errors with Xamarin you need to go through a process of elimination and get as much information as you can to diagnose the problem.

1. Rule out the environment

  • Close the IDE (visual studio or whatever) and delete the bin and obj directories of all your projects
  • If you are using the Emulator, Manually delete your project in the emulator and close the emulator
  • Restart and rebuild, and run the app again

I know this seems pedantic however you would be surprised how often this helps.

2. Get as much error information

  • When an exception is thrown, press continue and look for more clues in the output window
  • Also (and this should be a no-brainer), put a try catch around the line that throws the error and look for more information in the inner exceptions and stack trace

As an Example

try
{
    InitializeComponent();

    MainPage = new NavigationPage(new AppPage());
}
catch (Exception ex)
{ // break point here and inspect ex

}

Look at the exception in detail, look at all inner exceptions and of course look at the stack trace in depth

3. Check your Xaml with the compilation attribute on your pages and main App class.

  • This in most cases should be on anyway for several reasons, however it will do a compilation check on your Xaml and make sure there is no obvious mistakes.

Attribute

[XamlCompilation(XamlCompilationOptions.Compile)] 

From developer.xamarin.com : XAML Compilation

XAML can be optionally compiled directly into intermediate language (IL) with the XAML compiler (XAMLC).

XAMLC offers a number of a benefits:

  • It performs compile-time checking of XAML, notifying the user of any errors.
  • It removes some of the load and instantiation time for XAML elements.
  • It helps to reduce the file size of the final assembly by no longer including .xaml files.

XAMLC is disabled by default to ensure backwards compatibility. It can be enabled at both the assembly and class level by adding the XamlCompilation attribute.

4. Start commenting out Xaml in your offending page and App.Xaml

  • This is also a no brainer. If a TypeInizilationException is being thrown on every page, then its probably a style or resource located in your App.Xaml

  • Start commenting large parts of your App.Xaml and Pages out until something starts to work

I know this is extreme but its pretty quick to find the problem or at least isolate it

  1. Read How to create a Minimal, Complete, and Verifiable example

    • Only then ask the question. Include all the relevant code and nothing more, and please please include the full error in your question. Giving the type of error isn’t good enough. Error messages (although sometimes vague and cryptic) and stack traces in the majority of times include the information you need (and others) to identify the problem

Upvotes: 1

Related Questions