c# - Windows Phone 8.1 Silverlight 中的 Toast 通知参数

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

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

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

Step 2: Handle the app's "OnLaunched" event

When the user clicks on your toast or selects it through touch, the associated app is launched, firing its OnLaunched event.

Note If you do not include a launch attribute string in your toast and your app is already running when the toast is selected, the OnLaunched event is not fired.

This example shows the syntax for the override of the OnLaunched event, in which you will retrieve and act on the launch string supplied through the toast notification.

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

谁知道怎么解决? 谢谢

最佳答案

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

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

关于c# - Windows Phone 8.1 Silverlight 中的 Toast 通知参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275723/

相关文章:

c# - 发出异步休息请求

windows-phone-8.1 - 为什么 AppBarButtons 不响应 Windows Phone 8.1 RT 通用应用程序中的 Tapped 事件?

visual-studio-2013 - 发生意外的网络错误。应用列表无法刷新。请按刷新按钮重试

windows-phone-8 - 为什么我的 .Appx 文件没有显示在 Windows Phone 的“安装本地应用程序”菜单中?

c# - 有效地等待一个或多个资源可用

c# - 为什么不指定“ref”就可以从方法更改Struct的int []属性?

c# - 在 C# 中以编程方式在 ScriptBlock 中传递参数对象 (PSCredential)

c# - 完全禁用 ASP.NET Web 窗体中的 ViewState

image - 无法识别图像标题。 (来自 HRESULT : 0x88982F61) in Windows Phone 8 application 的异常

c# - MVVM:为 DataContract 类成员引发 PropertyChanged 事件