C# 当应用程序关闭时,如何在android作业服务中初始化mvvmcross表单IoC以使用注册服务?

C# 当应用程序关闭时,如何在android作业服务中初始化mvvmcross表单IoC以使用注册服务?,c#,android,forms,xamarin,mvvmcross,C#,Android,Forms,Xamarin,Mvvmcross,我的mvvmcross android表单应用程序中有一个作业服务,它检查服务器上的数据,如果是新帖子,则创建新通知。检查新帖子的代码正在使用中,所以我必须初始化mvvmcross IoC,但我得到了错误 [Service(Exported = true, Permission = "android.permission.BIND_JOB_SERVICE")] public class NotificationJobService : JobService {

我的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)
    }
}
[Service(Exported=true,Permission=“android.Permission.BIND\u JOB\u Service”)]
公共类通知JobService:JobService
{
私有静态只读字符串TAG=“ExampleJobService”;
公共覆盖bool OnStartJob(作业参数参数)
{
Log.Info(标签,“启动作业时:”+args.JobId);
多贝克地基(args);
返回true;
}
公共覆盖bool OnStopJob(JobParameters args)
{
返回true;
}
专用void dobackground(作业参数args)
{
新线程(()=>
{
var setupSingleton=MvxAndroidSetupSingleton.ensureSingleton可用(ApplicationContext);
setupSingleton.EnsureInitialized();
Mvx.IoCProvider.Resolve().ShowNewPostNotification(Newsfeed());
JobFinished(args,true);
}).Start();
}     
}
[活动(Label=“subrenderat20”,MainLauncher=true,Theme=“@style/MainTheme”,NoHistory=false,ScreenOrientation=ScreenOrientation.grait)]
公共类main活动:MvxFormsAppCompatActivity
{
创建时受保护的覆盖无效(捆绑包)
{        
base.OnCreate(bundle);
StartJob();
}
void StartJob()
{
Class javaClass=Class.FromType(typeof(NotificationJobService));
ComponentName ComponentName=新的ComponentName(这是javaClass);
JobInfo=new JobInfo.Builder(123,组件名称)
.SetMinimumLatency(20000)
.SETOVERRIDEADLINE(25000)
.SetPersisted(true)
.Build();
JobScheduler JobScheduler=(JobScheduler)GetSystemService(JobSchedulerService);
int result=jobScheduler.Schedule(信息);
if(result==JobScheduler.ResultSuccess)
{
日志信息(“主活动”、“作业计划”);
}
其他的
{
日志信息(“主要活动”、“已安排的作业文件”);
}
}
}
公共密封类AndroidSetup:MvxFormsAndroidSetup
{       
受保护的覆盖无效初始化ElastChance()
{
Mvx.IoCProvider.RegisterSingleton(typeof(IOperatingSystemService),newoperatingsystemservice());
RegisterSingleton(typeof(IMasterDetailService),newmasterdetailservice());
Mvx.IoCProvider.RegisterSingleton(typeof(INotificationService),newnotificationservice());
Mvx.IoCProvider.constructionandRegisterSingleton();
base.InitializeLastChance();//TODO删除(检查是否有效)
}
}
MvvmCross.Exceptions.MvxException:无法创建安装程序实例 --->MvvmCross.Exceptions.MvxException:在上找不到应用程序的安装类 中的MvvmCross.Core.MvxSetupExtensions.CreateSetup[TSetup]()[0x00019] :0 at 中的MvvmCross.Core.MvxSetup.Instance()[0x00015] :0 at 中的MvvmCross.Core.MvxSetupSingleton.CreateSetup()[0x00000] :0---内部异常结束 堆栈跟踪---在MvvmCross.Core.MvxSetupSingleton.CreateSetup()处 [0x00017]英寸:0英寸 MvvmCross.Core.MvxSetupSingleton.EnsureSingleton可用[TMvxSetupSingleton] ()[0x00045]英寸:0英寸 MvvmCross.Platforms.Android.Core.MvxAndroidSetupSingleton.EnsureSingletonAvailable (Android.Content.Context应用程序上下文)[0x00000]位于 :0 at 放弃20.表格.服务.通知JobService+c\u显示类别3\u 0.b\u 0 ()[0x0000b]英寸:0英寸 System.Threading.ThreadHelper.ThreadStart\u上下文(System.Object 状态)[0x00014]位于:0处 System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext ExecutionContext, System.Threading.ContextCallback回调,System.Object状态, System.SyncCTX中的[0x00071]布尔值 :0 at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext ExecutionContext, System.Threading.ContextCallback回调,System.Object状态, System.Syncctx)[0x00000]英寸 :0 at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext ExecutionContext, System.Threading.ContextCallback回调,System.Object状态) [0x0002b]英寸:0英寸 中的System.Threading.ThreadHelper.ThreadStart()[0x00008] :0


这个问题最初是在评论和帖子中回答的,希望能帮助他人

按照惯例,安装类必须命名为“setup”,因此需要将
AndroidSetup
重命名为
setup


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

查看MvvmCross源代码,MvvmCross似乎试图使用反射创建安装程序。它正在寻找一个名为“Setup”的类,这通常是Setup类名的约定。您可以尝试将安装类重命名为“安装”并重试吗?是的,它正在工作!非常感谢。很高兴能帮上忙,我会把它作为一个答案贴出来,这样其他人就可以得到帮助了