Windows Phone 8.1 Silverlight中的Toast通知参数

11

好的,所以我在我的8.1 SL项目中使用了新的ToastNotificationManager而不是旧的ShellToast。ShellToast有一个NavigationUri属性,使得它非常易用。

在新的toast中,根据这篇文章,你必须自己指定启动参数。然而,在App.xaml.cs中监听OnLaunched(LaunchActivatedEventArgs args)事件以获取参数在8.1 SL中似乎并不存在:

第二步:处理应用程序的"OnLaunched"事件

当用户单击您的toast或通过触摸选择时,相关联的应用程序将启动,触发其OnLaunched事件。

注意:如果您在toast中不包含启动属性字符串,并且您的应用程序在选择toast时已经运行,则不会触发OnLaunched事件。

此示例显示了OnLaunched事件的重写语法,其中您将检索并处理通过toast通知提供的启动字符串。

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    string launchString = args.Arguments

    ....
}

我的代码:

// Using the ToastText02 toast template.
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;

// Retrieve the content part of the toast so we can change the text.
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

//Find the text component of the content
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");

// Set the text on the toast. 
// The first line of text in the ToastText02 template is treated as header text, and will be bold.
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Heading"));
toastTextElements[1].AppendChild(toastXml.CreateTextNode("Body"));

// Set the duration on the toast
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");

//Launch params
string paramString = "{\"type\":\"toast\",\"param1\":\"12345\"}";
((XmlElement)toastXml.SelectSingleNode("/toast")).SetAttribute("launch", paramString);

// Create the actual toast object using this toast specification.
ToastNotification toast = new ToastNotification(toastXml);

// Set SuppressPopup = true on the toast in order to send it directly to action center without 
// producing a popup on the user's phone.
toast.SuppressPopup = true;

// Send the toast.
ToastNotificationManager.CreateToastNotifier().Show(toast);

有人知道如何解决这个问题吗? 谢谢


您可以直接为 Toast 提供导航参数。明天回到工作时我会了解详情。奇怪的是我们没有正确记录这个。 - Claus Jørgensen
谢谢,期待着! :) - robertk
如果您在Silverlight 8.1中使用ToastNotificationManager,由于SL在App.xaml中没有OnLoaded事件,您会使用什么代替它呢?我将其放在OnNavigatedTo中,但当点击Toast时似乎会调用两次。请参考下面的加载触发器来获取解决方案。 - gcoleman0828
1个回答

10

你的问题在于你设置了错误的launch参数。你应该直接将其设置为要导航到的页面。

var toastNavigationUriString = ""#/MainPage.xaml?param1=12345";
var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
toastElement.SetAttribute("launch", toastNavigationUriString);

1
在 WP 8.1 SilverLight 应用程序中,您需要在哪里定义 protected override void OnLaunched(LaunchActivatedEventArgs args) 方法?(在 App.xaml.cs 中它说没有要覆盖的方法) - Shishir Gupta
3
您不需要使用那种方法,那种方法只用于通用应用程序。对于Silverlight 8.1,您只需像我的示例一样直接传递完整的uri,指向您想要进入的页面即可。 - Claus Jørgensen
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Claus Jørgensen
@ClausJørgensen 谢谢 Claus,还有一件事 - 数据应该作为参数传递在 URI 中,对吗? - loop
如何在Windows Phone 8.1中使用JavaScript或Windows Runtime组件类进行处理? - Kishor Bikram Oli
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接