【问题标题】:System.TypeInitializationException: The type initializer for 'MyApp.Core.App' threw an exceptionSystem.TypeInitializationException:“MyApp.Core.App”的类型初始化程序引发异常
【发布时间】:2018-04-12 21:45:35
【问题描述】:

我正在编写一个 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);
    }
}

在 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>

【问题讨论】:

    标签: c# xamarin xamarin.forms typeinitializeexception


    【解决方案1】:

    请看TypeInitializationException的文档(见here

    作为类初始化器抛出的异常的包装器抛出的异常。

    您的类型初始化程序代码似乎抛出了异常。由于没有静态构造函数,我不会怀疑new Lazy&lt;T&gt;() 抛出异常,我猜问题出在

    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; } 
    
    public App ()
    {
        try
        {
            LoungeService = new LoungeService();
            LoginService = new LoginService();
        }
        catch(Exception e)
        {
            Debug.WriteLine(e);
            throw;
        }
    
        InitializeComponent();
    
        // ...
    }
    

    (或立即向您的DependencyService 注册)。

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 2017-08-17
      • 2016-11-30
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多