在Metro App中显示Toast notification

锺高翰
2023-12-01
 

 Toast notification是向用户显示一些有关App的即时消息。具体Toast notification是什么可以参考
 
http://msdn.microsoft.com/en-us/library/windows/apps/hh465360(v=VS.85).aspx
 创建Toast notification步骤:
 1.在Package.appxmanifest文件中设置ToastCapable="true",否则Toast Notification不能显示。
  <VisualElements  ForegroundText="light" BackgroundColor="#000000" ToastCapable="true">
  ForegroundText标签表示Toast notification消息文字的样式(有dark和light两种),BackgroundColor
  标签表示Toast notification的背景颜色。

 2.下面是显示Toast notification的代码:
  private void ShowToast()
  {
      // GetTemplateContent returns a Windows.Data.Xml.Dom.XmlDocument object containing
      // the toast XML

      XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04);
      XmlElement xe = toastXml.CreateElement("title");

      // You can use the methods from the XML document to specify all of the
      // required parameters for the toast
      XmlNodeList stringElements = toastXml.GetElementsByTagName("text");

      for (uint i = 0; i < stringElements.Length; i++)
      {
   stringElements.Item(i).AppendChild(toastXml.CreateTextNode("Pressed Screenshot key !"));
      }

      // Create a toast from the Xml, then create a ToastNotifier object to show
      // the toast
      ToastNotification toast = new ToastNotification(toastXml);

      // If you have other applications in your package, you can specify the AppId of
      // the app to create a ToastNotifier for that application

      ToastNotificationManager.CreateToastNotifier().Show(toast);

  }

 完成上面两步后,调用ShowToast方法就会在右下角显示一个Toast notification。

 类似资料: