在使用Xamarin.Forms之前,您必须调用Xamarin.Forms.Init()函数。

13

在我的app.xaml.cs文件中,我创建了一个新页面。

 public App()
  {
      InitializeComponent();
      MainPage = new NavigationPage(new WrapLayoutPage());
  }

这个页面调用了一个静态类,该类使用DependencyService执行一些任务。

抛出错误的那一行:

var tmpTable = SqLiteHelper.GetItem<TableX>("someId");

SqLiteHelper:

public static class SqLiteHelper
{
    private static readonly SQLiteConnection DatabaseConnection = DependencyService.Get<ISqLite>().GetConnection();
    private static readonly object Locker = new object();

    public static DbObjectV3 GetItem<T>(Guid inId) where T : DbObjectV3, new()
    {
        lock (Locker)
        {
            var tmpItem = DatabaseConnection.Table<T>().FirstOrDefault(inItem => inItem.Id == inId);
            tmpItem.IsNewObject = false;
            return tmpItem;
        }
    }
}

这给我抛出了一个TypeInitializationException,并且InnerException是:

在使用之前,您必须调用Xamarin.Forms.Init();

这与静态帮助类有关,因为在调用之前,我可以无问题地使用DependencyService!

作为主要启动器,我正在使用一个启动画面。在这个类中,我进行一些启动工作,这些工作依赖于DependencyService。

启动画面:

 [Activity(Theme = "@style/MyTheme.Splash", NoHistory = true, MainLauncher = true)]
    public class SplashScreen : Activity
    {
        static readonly string TAG = "X:" + typeof(SplashScreen).Name;

        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);
            Log.Debug(TAG, "SplashActivity.OnCreate");
        }
}

我的MainActivity:

 [Activity(Label = "FrameworkForms", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/MainActivityTheme", MainLauncher = false)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Xamarin.Forms.Forms.Init(this, bundle);
            App.ScreenWidth = (double)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
            LoadApplication(new App());
        }
    }

现在,将SplashScreen中的Activity更改为FormsAppCompatActivity后,我又遇到了另一个错误。

在隐藏键盘之前调用Forms.Init()

这里出了什么问题?


你能确保在 Android 和 Shared 项目中使用相同版本的 Xamarin.Forms 库吗? - Prashant Cholachagudda
@PrashantC 我已经升级到最新版本(2.3.3.175)。 - greenhoorn
不是这样的。我已经好几周没在上面工作了,但之前它肯定是在运行的。我正在尝试用一个新项目来复现它。 - greenhoorn
你尝试过将MainActivity的基类从FormsApplicationActivity更改为FormsAppCompatActivity吗?当我创建一个新项目时,后者是默认值,并且编译和运行都很好。 - Demitrian
@Demitrian 我已经尝试过了。AppCompat和Xamarin.Forms还有其他多个问题。这并不是一个好的建议...没有启动画面并且使用appcompat时它可以工作。但是启动画面会引起麻烦。 - greenhoorn
显示剩余4条评论
2个回答

12

这非常不幸。我在我的启动屏幕(SplashScreen)中使用了错误的OnCreate()方法。

我将启动屏幕(SplashScreen)更改为:

 [Activity(Theme = "@style/MyTheme.Splash", NoHistory = true, MainLauncher = true)]
    public class SplashScreen : Activity
    {
        static readonly string TAG = "X:" + typeof(SplashScreen).Name;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Forms.Forms.Init(this, savedInstanceState);
            Log.Debug(TAG, "SplashActivity.OnCreate");
        }


        protected override void OnResume()
        {
            base.OnResume();

            Task tmpStartupWork = new Task(() =>
            {
                Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
                StartUpTasks.InitializeDatabaseCreation();
                Log.Debug(TAG, "Working in the background - important stuff.");
            });

            tmpStartupWork.ContinueWith(inT =>
            {
                Log.Debug(TAG, "Work is finished - start MainActivity.");
                StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            }, TaskScheduler.FromCurrentSynchronizationContext());

            tmpStartupWork.Start();
        }
    }

很遗憾,Xamarin关于创建启动屏的文档使用了带有2个参数的OnCreate()方法!


没有参数的那个是我正在使用的,但我正在扩展应用程序。 - Lutaaya Huzaifah Idris
谢谢,你节省了我的时间 ;) - Badr Bellaj
保存我:Xamarin.Forms.Forms.Init(this, savedInstanceState); - Diego Venâncio

0

我也遇到了这个错误。错误的原因是我想要更改我的命名空间。从

public partial class AppDelegate : 
       global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate

我把它改成了:

using Xamarin.Forms.Platform.iOS;
public partial class AppDelegate : FormsApplicationDelegate

然后我看到了以下内容:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

我认为,调整一下也会很有用……没有更仔细地考虑它。于是我写下了:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    this.Init();

过了一会儿,我发现了错误,不得不费力地寻找原因。最终我找到并修复了它。也许这可以帮助某些人 :-) 干杯...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接