I am writing a Xamarin.Forms application. It was working just before, but now it is throwing an exception on this line of my Android project:
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
It throws the exception:
System.TypeInitializationException: The type initializer for 'MyApp.Core.App' threw an exception.
The constructor doesn't even get called. I don't even know what I did to cause this...does anyone have any idea?
Here is my code for App.xaml.cs
public partial class App : Application
{
public static INavigation Navigation
{
get;
set;
}
public static Lazy<LoungeListPage> LoungeListView { get; set; } = new Lazy<LoungeListPage>();
public static Lazy<LoungePage> LoungeView { get; set; } = new Lazy<LoungePage>();
public static Lazy<EditFencePage> FenceView { get; set; } = new Lazy<EditFencePage>();
public static Lazy<NewFencePage> NewFenceView { get; set; } = new Lazy<NewFencePage>();
public static ILoungeService LoungeService { get; set; } = new LoungeService();
public static ILoginService LoginService { get; set; } = new LoginService();
public App ()
{
InitializeComponent();
DependencyService.Register<PlatformDependentCode>();
DependencyService.Get<IPlatformDependentCode>().OnFirstPageLoaded = this.OnFirstPageLoaded;
var rootPage = new NavigationPage(new MyApp.Core.MainPage());
MainPage = rootPage;
App.Navigation = rootPage.Navigation;
}
protected override void OnStart ()
{
// Handle when your app starts
Plugin.Geolocator.CrossGeolocator.Current.DesiredAccuracy = 0.001;
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
private void OnFirstPageLoaded()
{
var deviceInfo = DependencyService.Get<IPlatformDependentCode>().GeneralPlatformDependent.GetDeviceInfo();
LoginService.InitializeDevice(deviceInfo);
}
}
And in App.xaml
<?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.Core.App">
<Application.Resources>
<!-- Application resource dictionary -->
</Application.Resources>
</Application>
Please see the documentation of
TypeInitializationException(see here)It seems as if there was an exception thrown by your type initializer code. Since there is no static constructor and I would not suspect
new Lazy<T>()to throw an exception, I'd guess that the issue is inSupposedly one of those throws an exception. You could check by creating the instances from your constructor and log that exception (temporarily).
(or register them with your
DependencyServiceright away).