Per un'applicazione Windows 8, in C#/Xaml, provo a registrare un'attività in background. È difficile da dire ma suppongo che il mio compito in background sia ben registrato ma quando faccio clic sul nome del mio task in background nella barra degli strumenti di debug la mia app smette di funzionare senza alcun messaggio.Impossibile registrare un'attività in background attivata in base al tempo
Ho controllato il registro nel Visualizzatore eventi e ho ricevuto: "L'attività in background con il punto di ingresso MyApp.Pages.SampleBackgroundTask e il nome Attività di background orario di esempio non è riuscita ad attivare con il codice di errore 0x80010008."
Ecco il mio codice:
private async void Button_Click_BackgroundTask(object sender, RoutedEventArgs e)
{
string entryPoint = "MyApp.Pages.SampleBackgroundTask";
string taskName = "Example hourly background task";
TimeTrigger hourlyTrigger = new TimeTrigger(60, false);
BackgroundTaskRegistration myBackGroundTaskRegistration = RegisterBackgroundTask(entryPoint, taskName, hourlyTrigger, null);
if(myBackGroundTaskRegistration != null)
AttachProgressAndCompletedHandlers(myBackGroundTaskRegistration);
}
public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint,
String name,
IBackgroundTrigger trigger,
IBackgroundCondition condition)
{
var taskRegistered = false;
var iter = Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks;
foreach (var item in iter)
{
var task = item.Value;
if (task.Name == name)
{
taskRegistered = true;
return item.Value as BackgroundTaskRegistration;
}
}
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration taskBack = builder.Register();
return taskBack;
}
private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task)
{
task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
}
private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
var progress = "Progress: " + args.Progress + "%";
}
private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
Debug.WriteLine("Hi");
}
ho aggiunto questo nel manifesto:
<Extension Category="windows.backgroundTasks" EntryPoint="MyApp.Pages.SampleBackgroundTask">
<BackgroundTasks>
<Task Type="timer" />
</BackgroundTasks>
</Extension>
ho dimenticato qualcosa?
Grazie per il vostro aiuto.
EDIT: ecco il codice del mio compito, all'interno di uno dei miei pagina del progetto:
public sealed class SampleBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
_deferral.Complete();
}
}
Bene, per cominciare, si ha un metodo 'async' senza un' await' in esso, che dovrebbe essere un avvertimento del compilatore. – Servy
MyApp.Pages.SampleBackgroundTask è un progetto separato all'interno della stessa soluzione? – Akinwale
No, l'attività si trova in una classe diversa all'interno di una pagina del mio progetto. Dovrebbe essere su un progetto diverso? Ho modificato la mia domanda per aggiungere il codice dell'attività. –