UWP 后台任务错误

Gui*_*aud 5 c# exception background-task uwp

我想创建一个在应用程序启动时启动的后台任务。为此,我使用应用程序触发器。

MainPage.xaml.cs

    var trigger = new ApplicationTrigger();
    BackgroundManagement.RegisterBackgroundTask("InternetBackgroundTask.InternetBackground", "Internet", trigger, null);
    await trigger.RequestAsync();
Run Code Online (Sandbox Code Playgroud)

后台管理.cs

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint,string taskName,IBackgroundTrigger trigger,IBackgroundCondition condition)
    {
        //
        // 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;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);
        

        if (condition != null)
        {

            builder.AddCondition(condition);
        }

        BackgroundTaskRegistration task = builder.Register();
       
        return task;
    }
Run Code Online (Sandbox Code Playgroud)

另一个项目上的 Mytask

namespace InternetBackgroundTask
{
public sealed class InternetBackground : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        System.Diagnostics.Debug.WriteLine("Run Background Task");
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,当我启动我的应用程序时,出现此错误:

在backgroundTaskHost.exe 中的0x776BE26B (KernelBase.dll) 处抛出异常:0x04242420(参数:0x31415927、0x5DE30000、0x003CED68)。

在backgroundTaskHost.exe 中的0x776BE26B 处抛出异常:Microsoft C++ 异常:内存位置0x003CDF18 处的EETypeLoadException。

在backgroundTaskHost.exe 中的0x776BE26B 处抛出异常:Microsoft C++ 异常:[重新抛出] 在内存位置0x00000000。

在backgroundTaskHost.exe 中的0x776BE26B 处抛出异常:Microsoft C++ 异常:内存位置0x003CDF18 处的EETypeLoadException。

我在我的项目中引用了我的后台任务,并且在我的清单中添加了我的后台任务

Tab*_*eek 1

有两件事从你的截图中并不明显:

您是否在 Package.appxmanifest 的“声明”下声明了后台任务?

第二:“InternetBackground.cs”是什么项目类型?它应该是一个 Windows 运行时组件