简体   繁体   English

Xamarin服务和表单初始化

[英]Xamarin Service and Forms Init

I'm writing a Xamarin Android application that consists of service and a page for configuration. 我正在编写一个Xamarin Android应用程序,其中包括服务和用于配置的页面。 The service starts after device boot (launched by a BroadcastReceiver ) and needs a database connection to read the configuration. 该服务在设备启动(由BroadcastReceiver启动)之后启动,并且需要数据库连接来读取配置。 I receive an error when the service tries to read the database: 服务尝试读取数据库时收到错误消息:

You MUST call Xamarin.Forms.Init(); 您必须调用Xamarin.Forms.Init(); prior to using it. 在使用它之前。

For the database connection, I'm using SQLite.Net . 对于数据库连接,我正在使用SQLite.Net I know that I need to call the Xamarin.Forms.Init() to use the database, but I don't know how I can do it. 我知道我需要调用Xamarin.Forms.Init()来使用数据库,但是我不知道该怎么做。 The Init() method needs context and Android.OS.Bundle as inputs, but in the BroadcastReceiver class, I have only the context. Init()方法需要上下文和Android.OS.Bundle作为输入,但是在BroadcastReceiver类中,我只有上下文。 How I can retrieve the bundle? 如何获取捆绑包? Is there another way to do that? 还有另一种方法吗?

This is the exception stacktrace: 这是异常堆栈跟踪:

 at Xamarin.Forms.Device.get_PlatformServices () at Xamarin.Forms.Device.GetAssemblies () at Xamarin.Forms.DependencyService.Initialize () at Xamarin.Forms.DependencyService.Get[T] (Xamarin.Forms.DependencyFetchTarget fetchTarget) at MyProject.Database.ConfigurationDBService..ctor () at MyProject.Utils.ConfigurationUtils.getConfig () at MyProject.InvoiceUploadService.OnStartCommand (Android.Content.Intent intent, Android.App.StartCommandFlags flags,System.Int32 startId) 

如问题注释中的SushiHangover所建议的,我通过在没有DependecyService的情况下运行数据库连接来解决了该问题。

Put: 放:

Xamarin.Forms.Init();

In your MainActivity.cs class in Android Project. 在Android Project中的MainActivity.cs类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM