我想创建一个后台任务,它在应用程序启动时启动.为此我使用Application Trigger.
MainPage.xaml.cs中
var trigger = new ApplicationTrigger();
BackgroundManagement.RegisterBackgroundTask("InternetBackgroundTask.InternetBackground", "Internet", trigger, null);
await trigger.RequestAsync();
BackgroundManagement.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;
}
Mytask在另一个项目上
namespace InternetBackgroundTask
{
public sealed class InternetBackground : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
System.Diagnostics.Debug.WriteLine("Run Background Task");
}
}
所以当我启动我的应用程序时,我有这个错误:
Exception thrown at 0x776BE26B (KernelBase.dll) in backgroundTaskHost.exe: 0x04242420 (parameters: 0x31415927, 0x5DE30000, 0x003CED68).
Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.
Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.
我在项目中引用了我的后台任务,并在清单中添加了后台任务
解决方法:
你的剪辑中有两件事并不明显:
您是否已在Package.appxmanifest中的“声明”下声明了后台任务?
第二个:
什么项目类型是“InternetBackground.cs”?它应该是Windows运行时组件