简体   繁体   English

Windows 8后台任务

[英]Windows 8 Background tasks

So I seem to be having some issues making a background task fire in a windows store app; 所以我似乎在使Windows商店应用中的后台任务启动时遇到一些问题; I have followed the white paper a tutorial and gone though the sample code from Microsoft and all iterations of my code seem to fail. 我已经按照白皮书中的教程进行了操作,并通过了Microsoft的示例代码以及我的代码的所有迭代似乎都失败了。 Visual Studio does not give me any errors the background task just doesn't fire, the purpose of the task is to fire every 70 minutes when there is internet connectivity. Visual Studio不会给我任何错误,即后台任务不会触发,该任务的目的是在有Internet连接时每70分钟触发一次。

The scope of the code below is it's in own project called Tasks, and the manifest (not for this project but the main project in the solution) is properly filled for a background task to be found in this class 下面代码的范围是在自己的项目Tasks中,清单(不是针对该项目,而是解决方案中的主项目)已正确填充,以便在此类中找到后台任务

class BackroundBuilder
{
  public BackroundBuilder()
  {
    this.RegisterTimeTriggerBackgroundTask();
  }

  //this is the code that registers my backround task to run a trigger
  //was added for testing.
  private bool RegisterTimeTriggerBackgroundTask()
  {
    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
    builder.Name = "Background task test";
    builder.TaskEntryPoint = "PostPage.xmal";
    // Run every 70 minutes if the device has internet connectivity
    IBackgroundTrigger trigger = new TimeTrigger(70, false);
    builder.SetTrigger(trigger);
    IBackgroundCondition condition = new
        SystemCondition(SystemConditionType.InternetAvailable);

    //this is the trigger it's set to fire when internet becomes available            
    IBackgroundTrigger Itrigger = new
        SystemTrigger(SystemTriggerType.InternetAvailable,true);
    builder.SetTrigger(Itrigger);

    builder.AddCondition(condition);
    IBackgroundTaskRegistration task = builder.Register();

    return true;
  }

  public async void Run(IBackgroundTaskInstance taskInstance)
  {
    BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();            

    //WindowsBlogReader.FeedDataSource updateAll = new WindowsBlogReader.FeedDataSource();
    //direct input for the test string is declared below but the updateAll declaration     
    // above is the one that will be used once the test works
    WindowsBlogReader.LiveTileTimeUpdate updateAll = new WindowsBlogReader.LiveTileTimeUpdae();

    //this is the test to see if the background task will fire
    //await was in front of the below statement but im injecting that String into a method
    //that is not setup for async the method being used once this works is an async 
    updateAll.update("Background task fired");
    //this update method adds a String too the list of Sting that's the live tile cycles though 
    _deferral.Complete();
  }     
}

this is the manifest xml code 这是清单xml代码

<Extension Category="windows.backgroundTasks" EntryPoint="Tasks.BackroundBuilder">
  <BackgroundTasks>
    <Task Type="systemEvent" />
    <Task Type="timer" />
  </BackgroundTasks>
</Extension>

Any help would be appreciated. 任何帮助,将不胜感激。 If this code isn't enough information I can make more available. 如果此代码不足,我可以提供更多信息。 There are no (known) issues with other parts of the app as all the features work when the app is running. 应用程序的其他部分没有(已知)问题,因为在运行该应用程序时,所有功能均有效。

您需要使BackroundBuilder类实现IBackgroundTask接口

Is this a typo: builder.TaskEntryPoint = "PostPage.xmal"; 这是错字builder.TaskEntryPoint = "PostPage.xmal"; Should it be: builder.TaskEntryPoint = "PostPage.xaml"; 应该是: builder.TaskEntryPoint = "PostPage.xaml"; ? I'd point the finger at that if SLaks' answer doesn't seem to fix the problem. 如果SLaks的答案似乎无法解决问题,我会指出。

I'd type this as a comment but I don't have enough rep to do that. 我将其输入为注释,但是我没有足够的代表来这样做。

TaskEntryPoint should be the name of your class (including namespace) which implements IBackgroundTask . TaskEntryPoint应该是实现IBackgroundTask类(包括名称空间)的名称。 Eg "BackgroundTasks.MyBackgroundTask". 例如“ BackgroundTasks.MyBackgroundTask”。
This should also be added in your manifest file. 这也应该添加到清单文件中。 Open Package.appxmanifest, go to the Declarations tab, add Background Tasks declaration and fill in the "Entry point" field with the same name (eg "BackgroundTasks.MyBackgroundTask"). 打开Package.appxmanifest,转到“声明”选项卡,添加“后台任务”声明,并使用相同的名称(例如“ BackgroundTasks.MyBackgroundTask”)填写“入口点”字段。
Also, your task needs to be in a separate Windows Runtime Component project and every class in that project has to be public and sealed . 同样,您的任务需要在一个单独的Windows Runtime Component项目中,并且该项目中的每个类都必须是public并且是sealed

Here is a quick tutorial. 是一个快速教程。
An extensive one can be found here . 这里可以找到广泛的一个。

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

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