猿问

System.TypeInitializationException:

我正在编写Xamarin.Forms应用程序。它之前就可以工作,但是现在在我的Android项目的这一行上引发了一个异常:


global::Xamarin.Forms.Forms.Init(this, bundle);

LoadApplication(new App());

它引发异常:


System.TypeInitializationException:“ MyApp.Core.App”的类型初始值设定项引发了异常。


构造函数甚至都不会被调用。我什至不知道我是怎么做的导致这一点的。。。有人知道吗?


这是我的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);

    }

}



繁星淼淼
浏览 326回答 1
1回答

郎朗坤

请参阅的文档TypeInitializationException(请参阅此处)作为包装引发的异常,该异常由类初始值设定项引发的异常包装。您的类型初始化程序代码似乎抛出了异常。由于没有静态构造函数,因此我不怀疑new Lazy<T>()会引发异常,因此我想问题出在public static ILoungeService LoungeService { get; set; } = new LoungeService();public static ILoginService LoginService { get; set; } = new LoginService();据说其中之一引发了异常。您可以通过从构造函数创建实例进行检查,并记录该异常(临时)。public static ILoungeService LoungeService { get; set; }public static ILoginService LoginService { get; set; }&nbsp;public App (){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; LoungeService = new LoungeService();&nbsp; &nbsp; &nbsp; &nbsp; LoginService = new LoginService();&nbsp; &nbsp; }&nbsp; &nbsp; catch(Exception e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine(e);&nbsp; &nbsp; &nbsp; &nbsp; throw;&nbsp; &nbsp; }&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; // ...}(或立即向他们注册DependencyService)。
随时随地看视频慕课网APP
我要回答