简体   繁体   English

UWP 后台任务与 ApplicationTrigger - 注册问题

[英]UWP Background Task with ApplicationTrigger - Register Problem

I'm trying to add a out-of-process background task to a UWP app.我正在尝试将进程外后台任务添加到 UWP 应用程序。 This should be triggered by an application trigger.这应该由应用程序触发器触发。 When trying to register, I get the following error message:尝试注册时,我收到以下错误消息:

Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Class 未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))

I called我打了电话

await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync()等待 Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync()

earlier.早些时候。

I've also tried declaring them in the AppxManifest, but that didn't work either, and I don't know what task type to specify either.我也尝试在 AppxManifest 中声明它们,但这也不起作用,我也不知道要指定什么任务类型。

        public static async Task RegisterBackupTaskAsync()
    {
        var requestStatus = await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync();

        if (requestStatus != BackgroundAccessStatus.AlwaysAllowed)
        {
            await DialogHelper.ShowMessageDialogAsync("Bitte erlauben Sie der App im Hintergrund augeführt zu werden, sonst kommt es zu Problemen mit dem Backup", "Achtung");
            return;
        }            

        //var condition = new SystemCondition(SystemConditionType.UserPresent);
        AppTriggerBackupTask = new ApplicationTrigger();

        var task = await RegisterBackgroundTaskAsync(typeof(BackupBackgroundTask).ToString(), nameof(BackupBackgroundTask), AppTriggerBackupTask);
    }

        public static async Task<BackgroundTaskRegistration> RegisterBackgroundTaskAsync(string taskEntryPoint,  string taskName, ApplicationTrigger trigger = null, IBackgroundCondition condition = null)
    {
        // Check for existing registrations of this background task.
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {

            if (cur.Value.Name == taskName)
            {
                // The task is already registered.
                return (BackgroundTaskRegistration)(cur.Value);
            }
        }

        

        // Register the background task.

        var builder = new BackgroundTaskBuilder();
        
        builder.Name = taskName;

        // in-process background tasks don't set TaskEntryPoint
        if (taskEntryPoint != null && taskEntryPoint != String.Empty)
        {
            builder.TaskEntryPoint = taskEntryPoint;
        }
        builder.SetTrigger(trigger);

        if (condition != null)
        {
            builder.AddCondition(condition);
        }

        try
        {
            BackgroundTaskRegistration task = builder.Register();
            return task;
        }
        catch (Exception ex)
        {
            await DialogHelper.ShowErrorDialogAsync(ex);
            return null;
        }            
    }

RegisterBackupTaskAsync is called in the OnLaunched Method in App.xaml.cs在 App.xaml.cs 的 OnLaunched 方法中调用 RegisterBackupTaskAsync

First, you might have some misunderstanding about background tasks in UWP.首先,您可能对 UWP 中的后台任务有一些误解。 There are two types of background tasks: Out-of-process background tasks and In-process background tasks .后台任务有两种类型: Out-of-process background tasksIn-process background tasks

  • Out-of-process background tasks : It is implemented as lightweight classes that implement the IBackgroundTask interface that the OS runs in a separate process (backgroundtaskhost.exe).进程外后台任务:它被实现为轻量级类,这些类实现了操作系统在单独进程中运行的 IBackgroundTask 接口(backgroundtaskhost.exe)。
  • In-process background tasks : the app and its background process run in the same process进程内后台任务:应用程序及其后台进程运行在同一个进程中

The taskEntryPoint property is for Out-of-process background tasks , it will set the class(in another project- a Windows Runtime Component) that performs the work of the background task. taskEntryPoint属性用于进程外后台任务,它将设置执行后台任务工作的类(在另一个项目中 - 一个 Windows 运行时组件)。

In-process background tasks don't need to set this property because the background task will be handled in the OnBackgroundActivated() even of the App.Xaml.cs .进程内后台任务不需要设置此属性,因为后台任务将在 App.Xaml.cs 的App.Xaml.cs OnBackgroundActivated()中处理。

Back to your scenario, you are trying to set the taskEntryPoint property for your background task.回到您的场景,您正在尝试为后台任务设置taskEntryPoint属性。 So this should be an Out-of-process background tasks .所以这应该是一个Out-of-process background tasks You need to create a Windows Runtime Component project in your solution and put the BackupBackgroundTask class in that project.您需要在解决方案中创建 Windows 运行时组件项目,并将BackupBackgroundTask class 放入该项目中。 Of course, you could choose not to set this property but you will need to handle the background activity code in OnBackgroundActivated().当然,您可以选择不设置此属性,但您需要在 OnBackgroundActivated() 中处理后台活动代码。

You could refer to these two documents about how to create Out-of-process background tasks and In-process background tasks .您可以参考这两个文档来了解如何创建Out-of-process background tasksIn-process background tasks Create and register an out-of-process background task and Create and register an in-process background task . 创建并注册进程外后台任务创建并注册进程内后台任务

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

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