2014-05-06 109 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