我有一个Window phone 8应用程序,我需要为此应用程序提供BackGroundAgent。我知道我们可以简单地在主应用程序中添加后台代理项目的引用,它工作正常。但我想通过Assembly.Load()在Main应用程序中动态加载它并使其工作。
public MainPage()
{
try
{
System.Reflection.Assembly.Load("BackGroundAgent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
StartPeriodicAgent();
}
private void StartPeriodicAgent()
{
periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
if (periodicTask != null)
{
ScheduledActionService.Remove(periodicTaskName);
}
periodicTask = new PeriodicTask(periodicTaskName);
periodicTask.Description = "This demonstrates a periodic task.";
try
{
ScheduledActionService.Add(periodicTask);
}
catch (SchedulerServiceException)
{
}
}
WMAppManifest.xml
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume" />
<ExtendedTask Name="TombStoneBackGroundAgent">
<BackgroundServiceAgent Name="TombStoneAgent" Source="BackGroundAgent.dll" Specifier="ScheduledTaskAgent" Type="BackGroundAgent.ScheduledAgent"/>
</ExtendedTask>
</Tasks>