2014-05-06 206 views
11

我想为Windows Phone 8.1开发一个包含本地“通知”的通用应用程序。显示本地Toast通知

我想要做的是在吐司控制扭结中向用户显示所有消息(错误,信息,警告)。 一切都在本地完成,无需通过标准通知系统。 有几个系统,在Windows Phone 8的工作:

但它不可能包含在Windows Phone 8.1的项目这些库。

有人知道另一种显示“本地”吐司的方法吗?

回答

7

您可以使用运行应用程序时显示的本地通知。

ToastTemplateType toastTemplateXml = ToastTemplateType.ToastImageAndText01; 
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplateXml); 

然后您就需要填充的GetTemplateContent

<toast> 
    <visual> 
     <binding template="ToastImageAndText01"> 
      <image id="img" src=""/> 
      <text id="txt"></text> 
     </binding> 
    </visual> 
</toast> 

供应在XML DOM你敬酒的内容返回的XML。该图像仅适用于Windows 8.1。

指定它的启动参数

((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"1\",\"param2\":\"2\"}"); 

创建敬酒对象:

ToastNotification toast = new ToastNotification(toastXml); 

终于显示敬酒。

ToastNotificationManager.CreateToastNotifier().Show(toast); 

另外,如果你想使用第三方控件来显示敬酒,那么你可以考虑写了Windows Phone 8.1的Silverlight应用程序。

+0

好的答案,它帮了我很多。我填写以下回复。 –

25

在@msimons响应和以下url的帮助下:http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868254.aspx我成功显示了我的通知。

对于那些谁需要它,这是我的最后一个方法:

private void ShowToastNotification(String message) 
    { 
     ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01; 
     XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); 

     // Set Text 
     XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); 
     toastTextElements[0].AppendChild(toastXml.CreateTextNode(message)); 

     // Set image 
     // Images must be less than 200 KB in size and smaller than 1024 x 1024 pixels. 
     XmlNodeList toastImageAttributes = toastXml.GetElementsByTagName("image"); 
     ((XmlElement)toastImageAttributes[0]).SetAttribute("src", "ms-appx:///Images/logo-80px-80px.png"); 
     ((XmlElement)toastImageAttributes[0]).SetAttribute("alt", "logo"); 

     // toast duration 
     IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); 
     ((XmlElement)toastNode).SetAttribute("duration", "short"); 

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

     // Create the toast notification based on the XML content you've specified. 
     ToastNotification toast = new ToastNotification(toastXml); 

     // Send your toast notification. 
     ToastNotificationManager.CreateToastNotifier().Show(toast); 
    } 

我一个普遍应用的Windows Phone 8.1上测试。

并且不要忘记编辑“Package.appxmanifest”并激活通知。 在应用程序的package.appxmanifest文件中声明了提升Toast通知的功能。如果您使用Microsoft Visual Studio清单编辑器,只需在“应用程序”选项卡的“通知”部分中将Toast功能选项设置为“是”即可。

+0

使用ms-appx:///Images/img.png表示法时,My Image未显示。任何想法?我的构建属性是:内容,始终复制。 – markwilde

+0

你的照片是在“Images”目录吗? 一个规则与烤面包的图像是“图片必须小于200 KB的大小和小于1024 x 1024个像素”(来源:http://msdn.microsoft.com/en-us/library/windows/apps/xaml /hh868254.aspx)。不知道如果你的图像更大,会发生什么。 –

+0

我有一个非常小的图像(80:80),所以只有几个字节的大小。在我部署的应用程序上显示商店图标,而不是我提供的自定义图标。 – markwilde