gre*_*orn 8 c# android xamarin
在我的app.xaml.cs中,我创建了一个新页面.
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new WrapLayoutPage());
}
Run Code Online (Sandbox Code Playgroud)
此页面调用静态类,该类使用DependencyService执行某些任务.
抛出错误的行:
var tmpTable = SqLiteHelper.GetItem<TableX>("someId");
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会抛出一个TypeInitializationExceptionInnerException:
你必须调用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");
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要活动:
[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());
}
}
Run Code Online (Sandbox Code Playgroud)
现在将SplashScreen中的Activity更改为FormsAppCompatActivity后,我收到另一个错误.
在隐藏键盘之前调用Forms.Init()
这是什么东西?
这是很不幸的。我OnCreate()在SplashScreen中使用了错误的方法。
我将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();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,Xamarin上有关创建初始屏幕的文档使用了带有2个参数的OnCreate()方法!
| 归档时间: |
|
| 查看次数: |
8981 次 |
| 最近记录: |