【发布时间】:2017-12-19 05:20:52
【问题描述】:
我在我的 xamarin 表单项目中使用 Prism。我能够在我的视图模型中使用依赖注入(构造函数注入)而没有任何问题。我还利用后台服务在后台推送长时间运行的任务.如何在我的后台服务中注入依赖项?当我尝试将接口对象作为参数传递给构造函数(SyncingBackgroundingCode)时,对象(SqliteService)为空。我已经注册并解析了依赖项注入容器中的对象。 如何处理这种情况?谁能提供一个例子或链接来实现这个场景?
这是我试图实现依赖注入的一段代码。
This is in Droid :-
public class AndroidSyncBackgroundService : Service
{
CancellationTokenSource _cts;
public override IBinder OnBind (Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
{
_cts = new CancellationTokenSource ();
Task.Run (() => {
try {
//INVOKE THE SHARED CODE
var oBackground = new SyncingBackgroundingCode();
oBackground.RunBackgroundingCode(_cts.Token).Wait();
}
catch (OperationCanceledException)
{
}
finally {
if (_cts.IsCancellationRequested)
{
var message = new CancelledTask();
Device.BeginInvokeOnMainThread (
() => MessagingCenter.Send(message, "CancelledTask")
);
}
}
}, _cts.Token);
return StartCommandResult.Sticky;
}
public override void OnDestroy ()
{
if (_cts != null) {
_cts.Token.ThrowIfCancellationRequested ();
_cts.Cancel ();
}
base.OnDestroy ();
}
}
This is in PCL:-
public class SyncingBackgroundingCode
{
public SQLiteConnection _sqlconnection;
SqliteCalls oSQLite = new SqliteCalls();
ISqliteService _SqliteService;
public SyncingBackgroundingCode(ISqliteService SqliteService)
{
//object is null
}
public async Task RunBackgroundingCode(CancellationToken token)
{
DependencyService.Get<ISQLite>().GetConnection();
await Task.Run (async () => {
token.ThrowIfCancellationRequested();
if (App.oSqliteCallsMainLH != null)
{
App.bRunningBackgroundTask = true;
oSQLite = App.oSqliteCallsMainLH;
await Task.Run(async () =>
{
await Task.Delay(1);
oSQLite.ftnSaveOnlineModeXMLFormat("Offline", 0);
oSQLite.SyncEmployeeTableData();
oSQLite.SaveOfflineAppCommentData();
oSQLite.SaveOfflineAdditionToFlowData();
await Task.Delay(500);
var msgStopSyncBackgroundingTask = new StopSyncBackgroundingTask();
MessagingCenter.Send(msgStopSyncBackgroundingTask, "StopSyncBackgroundingTask");
});
}
}, token);
}
}
【问题讨论】:
-
可能在容器有机会注册服务之前调用了 DependencyService.Get
()。确保它们以正确的顺序被触发。否则,我看不出有任何理由导致服务没有得到解决。此外,在程序集上使用 [assembly: Dependency(typeof(SQLitService))] 注册服务可能是个好主意,这可确保您的服务始终在您的代码有机会解析之前注册。
标签: dependency-injection xamarin.forms prism background-service