我正在尝试从后台任务中的 toast 通知中检索参数,但是 Microsoft 网站上推荐的方法不起作用。
我在我的主 UWP 应用程序的 OnInitialize() 方法中注册如下任务,如下所示:
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name.Equals(taskName))
{
task.Value.Unregister(true);
}
}
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
{
Name = taskName,
TaskEntryPoint = "AppNamespace.GetQuestionsBackground"
};
我发送到设备的 toast 通知如下:
var toast = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" +
$"<toast launch=\"questionId:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\">" +
"<visual><binding template=\"ToastImageAndText02\">" +
"<text id=\"1\">New Question</text>" +
"<text id=\"2\">There is a question waiting to be answered</text>" +
"<image id=\"1\" src=\"ms-appx:///Assets/DailyQuestion32.png\" />" +
"</binding></visual>" +
"<actions>" +
$"<action activationType=\"foreground\" content=\"Answer now\" arguments=\"now:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\"/>" +
$"<action activationType=\"background\" content=\"Answer later\" arguments=\"later:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\"/>" +
"</actions>" +
"</toast>";
后台任务(位于单独的 Windows 运行时组件项目中)如下:
public sealed class GetQuestionsBackground : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferal = taskInstance.GetDeferral();
this.container = this.diBootStaraaper.BootStrap(applicationData);
var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
if (details.Argument != null)
{
var argList = details.Argument.Split(':');
IDataProvider dataProvider = DependencyHelper.Resolve<IDataProvider>();
await dataProvider.SaveQuestion(argList[1], argList[2], argList[3]);
dataProvider.SaveSettings();
}
_deferal.Complete();
}
}
包清单中还注册了后台任务,如下所示:
<Extension Category="windows.backgroundTasks" EntryPoint="AppNamespace.GetQuestionsBackground">
<BackgroundTasks>
<Task Type="systemEvent" />
</BackgroundTasks>
</Extension>
details 变量只是空的但是 if 语句条件为真?此外,当我在代码中断点并单步执行时,Visual Studio 无法查看 taskInstance.triggerDetails 的当前状态......
任何帮助将不胜感激,谢谢!
更新:当我单击 toast 通知按钮并查看 Visual Studio 中的变量时,我已在后台任务中设置断点,并且我可以在本机实现的 _PiEventDetails 中的 m_argument 变量下看到我想要的数据。所以我知道数据在那里,但吸气剂似乎没有工作。