質問

Reading up on background agents in Windows Phone. All guides say I should start with creating a new project specifically for the agent. Is that a requirement? Cite place.

The bigger question is - how does the framework find the class that implements the scheduled task? AFAIK, starting a background task involves calling ScheduledActionService.Add() passing a ScheduledAction-derived object as a parameter. Nowhere in here can I see any pointer to the identity of task's implementation. Neither are tasks registered in the manifest.

役に立ちましたか?

解決 2

Neither are tasks registered in the manifest.

They are. See the "BackgroundServiceAgent" element in your manifest file: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769509(v=vs.105).aspx

In the end, I don't know if creating a separate project is a requirement. By manually adding the line in the manifest and pointing to a class in the main project, I don't see what could technically prevent the background agent from working. I haven't tried though. Still, putting the background agent in a separate assembly can be convenient: the memory limit for agents is ridiculously low, so not having to load the main project and its dependencies can probably save a few hundreds KB of memory

他のヒント

Yes, the background task needs to be located in separate project. In theory the background task could be in the original project, but then it will easily hit the memory cap, because the whole project needs to be loaded in order to run just the background task code.

It also needs to be added into your main app project using "Add reference".
The project with background task also need to have class, that is a child of ScheduledTaskAgent class.

Then (in case of WP8 app) when your app is built using Visual Studio and if the Visual Studio finds such referenced project with ScheduledTaskAgent, new entry is injected into the WMAppManifest.xml to tell the app launcher that this app has background task available and in case user actually register this task for running, WP OS should start the assembly located in app manifest. This is how it looks in the manifest file:

<Tasks>
  <DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume" />
  <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="EreaderBackgroundAgent" Source="EreaderBackgroundAgent" Type="EreaderBackgroundAgent.ScheduledAgent" />
  </ExtendedTask>
</Tasks>

The DefaultTask entry just describes the default app entry point, the ExtendedTask is the entry for background task.

Note also that when developing apps for Windows Phone 7.5, this entry had to be added manually into the manifest and it was a common problem that devs published app without this entry in manifest.

Also another interesting discovery, if you reference in your main project -> libraryA, that is referencing another libraryB, and only the libraryB implements the ScheduledTaskAgent, then Visual Studio won't add the entry into manifest, because it cannot check indirectly referenced projects - if the libraryA has no ScheduledTaskAgent implementation, the library is not considered as background task library. But, you can again add the entry to libraryA into manifest manually and it will work just fine.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top