问题 Windows Phone 8.1 Silverlight中的Toast通知参数


好的,所以我在8.1 SL项目中使用新的ToastNotificationManager而不是旧的ShellToast。 ShellToast在toast消息上有NavigationUri,这非常容易。

在新的祝酒词中你必须自己指定启动参数 这个 文章。然而,似乎8.1 SL没有这个事件 OnLaunched(LaunchActivatedEventArgs args) 你应该在App.xaml.cs中监听参数:

第2步:处理应用程序的“OnLaunched”事件

当用户点击你的吐司或通过触摸选择它时,   启动相关应用程序,启动其OnLaunched事件。

注意如果您的Toast中未包含启动属性字符串   并且当选择Toast时,您的应用程序已在运行   OnLaunched事件未被触发。

此示例显示了OverLaunched的覆盖语法   事件,您将在其中检索并执行启动字符串   通过吐司通知提供。

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);

谁知道怎么解决这个问题? 谢谢


5824
2018-04-24 17:24


起源

您可以直接向toast提供导航参数。我明天回去工作的时候会得到细节。奇怪的是我们没有正确记录这一点。 - Claus Jørgensen
谢谢期待! :) - robertk
如果您在Silverlight 8.1中使用ToastNotificationManager,那么您使用的是什么而不是OnLoaded事件,因为SL在App.xaml中没有?我在OnNavigatedTo中有它,但是当点击toast时它似乎调用它两次我使用下面的加载触发器的答案。 - gcoleman0828


答案:


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

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

10
2018-04-25 10:25



这真的很好,谢谢! - robertk
你需要在哪里定义 protected override void OnLaunched(LaunchActivatedEventArgs args) WP 8.1 SilverLight应用程序中的方法? (在App.xaml.cs中,它说没有要覆盖的方法) - Shishir Gupta
@ShishirGupta你没有,该方法专门用于通用应用程序。对于Silverlight 8.1,您只需将完整的uri直接传递给您要输入的页面,根据我的示例。 - Claus Jørgensen
@ClausJørgensen如果我们不想导航到任何页面但想要使用toast发送一些数据怎么办? - loop
@loop你必须 总是 单击Toast时导航到应用中的页面。否则它将无法通过认证。 - Claus Jørgensen