Adding UWP Background task causes AppManifest errors

后端 未结 5 1398
天涯浪人
天涯浪人 2020-12-10 19:09

I\'ve been trying to add an out of process background task to my project to update my App\'s live tile. The task needs to make a database call to get some data, then process

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 19:15

    I hit the same issue. Unfortunately adding audio is not a solution as this will be a requirement for the windows store, not a good idea. Setting minimum target version gives you very limited Windows Store functionality and less UWP features.

    Get started by using this link and follow it very carefully:

    https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

    To summarise:

    • Ensure you have a good copy of your project, my manifiest file became corrupted during experimentation and I needed the Package.appxmanifest backup copy.
    • Delete all references and code relating to anything to do with background tasks from your project.
    • In your solution create a new Windows Universal / Windows Runtime Component project.
    • Name the project BackgroundTasks (as an example)
    • Right click references and Manage Nuget Packages
    • Select the installed tab and Microsoft.NETCore.UniversalWindowsPlatform
    • Change the version from 6.1.4 to 6.0.8 (6.1.4 seems to be a preview version which causes extra unwanted references to be added everytime VS is loaded).
    • Change Class1.cs name to MyBackgroundTask.cs
    • Select the BackgroundTasks project Target / Min version to Fall Creators Update
    • Right click your solution / Project Dependancies. Select BackgroundTasks and check the build order is correct, mine is set for BackgroundTasks to build before the main project.
    • Refer to the sample code at UWP Samples to create the sample background task.
    • Change the Package.appxmanifest file as described on the above link.
    • Build the solution and you should be in business.

    Personally I recommend to use an out-of-process background task to eliminate any possibly that if the background task crashes it does not affect the app from running, also much more flexible.

    As a tip your BackgroundTaskBuilder will reference as strings:

     builder.Name = "MyBackgroundTask";
     builder.TaskEntryPoint = "BackgroundTasks.MyBackgroundTask";
    

    I find making background tasks in UWP quite tedious, but once done well worth the effort.

提交回复
热议问题