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

时间:2017-01-04 10:10:45

标签: c# android xamarin

在我的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;
        }
    }
}

这会抛出一个带有InnerException的TypeInitializationException

  

你必须调用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");
        }
}

我的主要活动:

 [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()

这是什么东西?

2 个答案:

答案 0 :(得分:7)

这非常不幸。我在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()方法!

答案 1 :(得分:0)

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

PARTITION

我把它改为:

INSERT dbo.[item_views]
(
    a,
    c1,
    c2,
    l,
    Cookie_ID,
    source
)
PARTITION
(
    @partition1
)
SELECT Query_a1,
       Query_c1,
       Query_c2,
       Query_l,
       Cookie_ID,
       "abcd" AS source
FROM dbo.log
WHERE DateStamp.Date == "2017-01-01";

然后我看到了以下内容:

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

我想,调整它也是有用的...而不用考虑更准确。我写道:

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

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