使用 Prism 在 Xamarin Forms 的后台服务中实现依赖注入

Implement dependency injection in background services in Xamarin Forms using Prism

我在我的 xamarin 表单中使用 Prism project.I 能够在我的视图模型中使用依赖注入(构造函数注入)而无需任何 problems.I 我也在使用后台服务来推送background.How 中的长 运行 任务是否在我的后台服务中注入依赖项?当我尝试将接口对象作为参数传递给构造函数(SyncingBackgroundingCode)时,对象(SqliteService)是 null.I 已经注册并解析了依赖注入容器中的对象。 如何处理这种情况?有人可以提供示例或 link 来实现这种情况吗?

这是我尝试实现依赖注入的代码段。

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);
                    }
                }

不幸的是,Xamarin 和 Xamarin Forms 没有提供像 Prism 这样的框架来处理 IoC 场景。不过,您可以通过多种方式处理此问题。

首先,Container 是后台服务中 PrismApplication 上的 public 属性,您可以执行以下操作:

public class FooBackgroundService
{
    private App _app => (App)Xamarin.Forms.Application.Current;

    private void DoFoo()
    {
        var sqlite = _app.Container.Resolve<ISQLite>();
    }
}

另一种稍微复杂的方法是使用 ServiceLocator 模式。您可能有如下内容:

public static class Locator
{
    private static Func<Type, object> _resolver;

    public static T ResolveService<T>() => 
        (T)_resolver?.Invoke(typeof(T));

    public static void SetResolver(Func<Type, object> resolver) => 
        _resolver = resolver;
}

在您的应用中,您只需设置解析器即可。 Prism 实际上对 ViewModel 定位器做了类似的事情,然后允许它注入 NavigationService 的正确实例。

public class App : PrismApplication
{
    protected override void OnInitialized()
    {
        SetServiceLocator();
        NavigationService.NavigateAsync("MainPage");
    }

    protected override void RegisterTypes()
    {
        // RegisterTypes
    }

    private void SetServiceLocator()
    {
        Locator.SetResolver(type => Container.Resolve(type, true));
    }
}

最后,您的服务将简单地引用服务定位器,例如:

public class BarBackgroundService
{
    public void DoBar()
    {
        var sqlite = Locator.ResolveService<ISQLite>();
        // do foo
    }
}