我想实现Toast和Tile通知。我的通知必须符合某些条件,例如它需要能够在我的应用关闭时运行。例如,生日提醒可以在应用关闭时在后台运行。
我发现的样本:
ShellToast toast = new ShellToast();
toast.Title = "Toast Title: ";
toast.Content = "TEST";
toast.Show();
以上示例在应用程序运行时有效。这是我的代码:
private void StartPeriodicAgent()
{
// Variable for tracking enabled status of background agents for this app.
agentsAreEnabled = true;
// Obtain a reference to the period task, if one exists
periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
// If the task already exists and background agents are enabled for the
// application, you must remove the task and then add it again to update
// the schedule
if (periodicTask != null)
{
RemoveAgent(periodicTaskName);
}
periodicTask = new PeriodicTask(periodicTaskName);
periodicTask.ExpirationTime = System.DateTime.Now.AddDays(1);
// The description is required for periodic agents. This is the string that the user
// will see in the background services Settings page on the device.
periodicTask.Description = "This demonstrates a periodic task.";
// Place the call to Add in a try block in case the user has disabled agents.
ScheduledActionService.Add(periodicTask);
}
private void RunBackgroundWorker()
{
//PhoneCallTask calltask = new PhoneCallTask();
//calltask.PhoneNumber = "03336329631";
//calltask.DisplayName = "arslan";
//calltask.Show();
BackgroundWorker backroungWorker = new BackgroundWorker();
backroungWorker.DoWork += ((s, args) =>
{
Thread.Sleep(10000);
});
backroungWorker.RunWorkerCompleted += ((s, args) =>
{
this.Dispatcher.BeginInvoke(() =>
{
var toast = new ToastPrompt
{
Title = "Simple usage",
Message = "Message"
};
toast.Show();
}
);
});
backroungWorker.RunWorkerAsync();
}
但我没有收到任何通知。任何人都可以告诉我如何设置应用程序未运行时有效的通知吗?
答案 0 :(得分:2)
BackgroundWorker与您计划定期运行的计划任务不同。
添加Windows Phone计划任务代理,并在该项目中编写代码逻辑,以调用生成吐司所需的调用。
protected override void OnInvoke(ScheduledTask task)
{
------------------------------------------------
Your code for scheduled task running.
}
完成代码后,一旦计划任务的工作结束,您就可以调用NotifyComplete()来指示。
而 backgroundworker 只是在一个单独的线程中运行您的代码,除了事实之外,没有与计划任务的关联;您可以在计划任务中使用后台线程。
为了在主应用程序和计划任务之间共享您的逻辑: - 创建一个单独的项目并在其中放入可重用/共享代码。 请参阅主应用程序和计划任务中的此信息以共享/访问变量。
在单独的项目中使用IsolatedStorageFile和Mutex,并在两者之间共享DLL
*计划任务示例的随机参考:http://wildermuth.com/2011/9/6/Periodic_Agents_on_Windows_Phone_7_1 *