11

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

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

第 2 步:处理应用的“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);

任何人都知道如何解决这个问题?谢谢

4

1 回答 1

10

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

var toastNavigationUriString = ""#/MainPage.xaml?param1=12345";
var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
toastElement.SetAttribute("launch", toastNavigationUriString);
于 2014-04-25T10:25:49.240 回答