gpt4 book ai didi

c# - 你必须调用 Xamarin.Forms.Init();使用前

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:13 25 4
gpt4 key购买 nike

在我的 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:

You MUST call Xamarin.Forms.Init(); prior to using it

它在某种程度上与静态帮助程序类相关,因为在该调用之前,我可以毫无问题地使用 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 :

 [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 后,出现另一个错误。

Call Forms.Init() before Hide Keyboard

这是什么东西?

最佳答案

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

关于c# - 你必须调用 Xamarin.Forms.Init();使用前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com