首页 > 解决方案 > 如何在应用程序关闭时在android作业服务中初始化mvvmcross表单IoC以使用注册服务?

问题描述

我的 mvvmcross android 表单应用程序中有工作服务,该应用程序正在检查来自服务器的数据,如果是新帖子,则创建新通知。检查新帖子的代码正在使用中,所以我必须初始化 mvvmcross IoC,但那时我遇到了错误。

 [Service(Exported = true, Permission = "android.permission.BIND_JOB_SERVICE")]
    public class NotificationJobService : JobService
        {
            private static readonly string TAG = "ExampleJobService";

            public override bool OnStartJob(JobParameters args)
            {
                Log.Info(TAG, "on start job: " + args.JobId);

                DoBackgroundWork(args);

                return true;
            }

            public override bool OnStopJob(JobParameters args)
            {
                return true;
            }

            private void DoBackgroundWork(JobParameters args)
            {
                new Thread(() =>
                {
                    var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
                    setupSingleton.EnsureInitialized();

                    Mvx.IoCProvider.Resolve<INotificationService>().ShowNewPostNotification(new Newsfeed());

                    JobFinished(args, true);

                }).Start();
            }     
        }


    [Activity(Label = "SurrenderAt20", MainLauncher = true, Theme = "@style/MainTheme", NoHistory = false, ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : MvxFormsAppCompatActivity<AndroidSetup, CoreApp, App>
    {

    protected override void OnCreate(Bundle bundle)
    {        
        base.OnCreate(bundle);

        StartJob();
    }

    void StartJob()
    {
        Class javaClass = Class.FromType(typeof(NotificationJobService));
        ComponentName componentName = new ComponentName(this, javaClass);

        JobInfo info = new JobInfo.Builder(123, componentName)
           .SetMinimumLatency(20000)
           .SetOverrideDeadline(25000)
           .SetPersisted(true)
           .Build();


        JobScheduler jobScheduler = (JobScheduler)GetSystemService(JobSchedulerService);
        int result = jobScheduler.Schedule(info);
        if (result == JobScheduler.ResultSuccess)
        {
            Log.Info("MainActivity", "Job Sheduled");
        }
        else
        {
            Log.Info("MainActivity", "Job Filed Sheduled");
        }
    }
}

public sealed class AndroidSetup : MvxFormsAndroidSetup<CoreApp, App>
{       
    protected override void InitializeLastChance()
    {
        Mvx.IoCProvider.RegisterSingleton(typeof(IOperatingSystemService), new OperatingSystemService()); 
        Mvx.IoCProvider.RegisterSingleton(typeof(IMasterDetailService), new MasterDetailService());       
        Mvx.IoCProvider.RegisterSingleton(typeof(INotificationService), new NotificationService());       
        Mvx.IoCProvider.ConstructAndRegisterSingleton<IMvxAppStart, MvxAppStart<RootPageViewModel>>();

        base.InitializeLastChance(); //TODO remove (check if work)
    }
}

MvvmCross.Exceptions.MvxException: 无法创建设置实例 ---> MvvmCross.Exceptions.MvxException: 在 <904768743fbc44a983fee28742390539> 中的 MvvmCross.Core.MvxSetupExtensions.CreateSetup[TSetup] () [0x00019] 中找不到应用程序的设置类: 0 在 MvvmCross.Core.MvxSetup.Instance () [0x00015] in <904768743fbc44a983fee28742390539>:0 在 MvvmCross.Core.MvxSetupSingleton.CreateSetup () [0x00000] 在 <904768743fbc44a983fee239> 内部异常堆栈跟踪中的 0 8742 - at MvvmCross.Core.MvxSetupSingleton.CreateSetup () [0x00017] in <904768743fbc44a983fee28742390539>:0 at MvvmCross.Core.MvxSetupSingleton.EnsureSingletonAvailable[TMvxSetupSingleton] () [0x00045] in <904768743fbc44a983fee28742390539>:0 at MvvmCross.Platforms.Android.Core .MvxAndroidSetupSingleton.EnsureSingletonAvailable (Android.Content.Context applicationContext) [0x00000] in <904768743fbc44a983fee28742390539>:0 at Surrender_20.Forms.Services.NotificationJobService+<>c__DisplayClass3_0.b__0 () [0x0000b] in <3bf0784f2f454fc395358149cf734980>:0 at System.Threading.ThreadHelper .ThreadStart_Context(System.Object 状态)[0x00014] 在 System.Threading.ExecutionContext.RunInternal 的 0 处(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback 回调,System.Object 状态,System.Boolean preserveSyncCtx)[0x00071]在:0 在 System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback 回调,System.Object 状态,System.Boolean preserveSyncCtx)[0x00000] 在:0 在 System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, System.Object 状态) [0x0002b] 在:0 在 System.Threading.ThreadHelper.ThreadStart () [0x00008] 在: 0

标签: c#androidformsxamarinmvvmcross

解决方案


问题最初在评论中回答并作为答案发布,希望对其他人有所帮助。

按照惯例,您的安装类必须命名为“Setup”,因此您需要重命名AndroidSetupSetup

背景:看Mvvmcross源码,初始化逻辑使用反射和约定来初始化自己。它正在寻找一个名为“Setup”的类。在您的情况下, MvvmCross 找不到您的安装程序类并抛出了您在堆栈跟踪中读取的异常MvvmCross.Exceptions.MvxException: Could not find a Setup class for application


推荐阅读