【问题标题】:Toast Notification parameters in Windows Phone 8.1 SilverlightWindows Phone 8.1 Silverlight 中的 Toast 通知参数
【发布时间】:2014-04-24 17:24:47
【问题描述】:

好的,我在 8.1 SL 项目中使用新的 ToastNotificationManager,而不是旧的 ShellToast。 ShellToast 在 toast 消息中有 NavigationUri,这让它变得非常简单。

在新的 toasts 中,您必须根据this 文章自行指定启动参数。但是,似乎 8.1 SL 没有您应该在 App.xaml.cs 中为参数监听的事件 OnLaunched(LaunchActivatedEventArgs args)

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

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

注意如果你的 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 提供导航参数。我明天回去工作时会得到详细信息。奇怪的是,我们没有正确记录这一点。
  • 感谢期待! :)
  • 如果您在 Silverlight 8.1 中使用 ToastNotificationManager,那么您使用什么来代替 OnLoaded 事件,因为 SL 在 App.xaml 中没有该事件?我在 OnNavigatedTo 中有它,但是单击 toast 时它似乎调用了两次我使用下面的加载触发器的答案。??

标签: c# windows-phone-8 windows-phone-8.1 windows-phone-8-sdk


【解决方案1】:

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

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

【讨论】:

  • 您需要在 WP 8.1 SilverLight 应用程序中哪里定义protected override void OnLaunched(LaunchActivatedEventArgs args) 方法? (在 App.xaml.cs 中它说没有方法可以覆盖)
  • @ShishirGupta 你不知道,这种方法专门用于通用应用程序。对于 Silverlight 8.1,您只需将完整的 uri 传递给您要输入的页面,就像我的示例一样。
  • @ClausJørgensen 如果我们不想导航到任何页面但想使用 toast 发送一些数据怎么办?
  • @loop 您必须始终在点击 Toast 时导航到应用中的页面。否则无法通过认证。
  • @ClausJørgensen 感谢克劳斯,还有一件事 - 数据应该作为参数在 uri 中传递对吗?
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
相关资源
最近更新 更多