我使用 C# 和 Windows Phone 8.1 作为通用应用程序。我可以通过以下代码从后台发送通知:
void ShowNotification(string title, string text)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList textElements = toastXml.GetElementsByTagName("text");
textElements[0].AppendChild(toastXml.CreateTextNode(title));
textElements[1].AppendChild(toastXml.CreateTextNode(text));
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
}
我希望当用户在操作中心点击这些通知时,我在通知中编写的消息显示在我的主页中的文本 block 上,但我不知道如何设置参数来执行此操作。我在 MainPage 中使用了此代码,但参数为空:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e != null && e.Parameter != null)
{
Debug.WriteLine("MainPage.OnNavigatedTo.Parameter: " + e.Parameter.ToString());
}
}
谢谢
最佳答案
您需要指定应用程序启动参数,例如:
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");
然后在应用程序的 onlaunched 事件中,使用您获得的参数导航到主页,如下所示:
rootFrame.Navigate(typeof(MainPage), args.Arguments);
关于c# - 设置通知参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731324/