2014-04-24 75 views
11

好吧,我在8.1 SL项目中使用新的ToastNotificationManager而不是旧的ShellToast。 ShellToast在Toast消息上有NavigationUri,这使得它非常容易。Windows Phone 8.1中的Toast通知参数Silverlight

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

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

当用户点击您的面包片或通过触摸选择它时,会启动 关联的应用程序,从而启动其OnLaunched事件。

注意如果不包括发射属性字符串在祝酒词 和您的应用程序已经运行选择敬酒时,该 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); 

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

+0

您可以直接为烤面包提供导航参数。当我明天回去工作时,我会得到细节。奇怪的是,我们没有正确记录这一点。 –

+0

感谢您的期待! :) – robertk

+0

如果您在Silverlight 8.1中使用ToastNotificationManager,那么您使用的是什么而不是OnLoaded事件,因为SL在App.xaml中没有该事件?我把它放在OnNavigatedTo中,但是当点击吐司时它似乎调用了两次我在下面的加载触发器中使用了答案。 – gcoleman0828

回答

10

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

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

这真的很好,谢谢! – robertk

+1

您需要在WP 8.1 SilverLight应用程序中定义'protected override void OnLaunched(LaunchActivatedEventArgs args)'方法? (在App.xaml.cs中,它说没有方法可以覆盖) –

+3

@ShishirGupta你不这样做,该方法专门用于通用应用程序。对于Silverlight 8.1,您只需将其全部uri传递给您想要输入的页面,如同我的示例一样。 –

相关问题