2016-05-31 19 views
5

Nella mia app, desidero informare l'utente quando un'azione particolare è stata eseguita, come il record aggiornato correttamente o il nuovo record aggiunto, ma non c'è un controllo integrato che possa visualizzare tali informazioni. C'è qualcosa di simile a Android Toast.makeText per UWP?Come creare notifiche informative sul brindisi nell'app UWP

risposta

13

Sì, UWP ha Toast Notifiche :)

Ecco il codice di esempio per visualizzare semplice comunicazione:

private void ShowToastNotification(string title, string stringContent) 
{ 
     ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier(); 
     Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); 
     Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text"); 
     toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title)); 
     toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent)); 
     Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); 
     Windows.Data.Xml.Dom.XmlElement audio = toastXml.CreateElement("audio"); 
     audio.SetAttribute("src", "ms-winsoundevent:Notification.SMS"); 

     ToastNotification toast = new ToastNotification(toastXml); 
     toast.ExpirationTime = DateTime.Now.AddSeconds(4); 
     ToastNotifier.Show(toast); 
} 

In questo articolo potete trovare come personalizzarlo:

https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts

+0

Ma non è una notifica informativa per il brindisi, se l'utente la ignora di quanto non vada nel centro di notifica che non desidero. –

+0

Sotto la riga aggiungere: toast.ExpirationTime = DateTime.Now.AddSeconds (4); Questo mostrerà la notifica per 4 secondi e poi sparirà anche dal Centro operativo. –

+0

Ho modificato la mia risposta. –

0

Ecco come realizzare semplici makeText come Android:

private Windows.UI.Xaml.Controls.Frame frame; 
    private Windows.UI.Xaml.Controls.Page page; 
    private Ribo.Smart.App.UserControls.Components.Common.Toast toast; 

    private DispatcherTimer timer = new DispatcherTimer(); 
    void timer_Tick(object sender, object e) 
    { 
     if (toast != null) 
      ((Panel)page.FindName("layoutRoot")).Children.Remove(toast); 

     toast = null; 

     timer.Stop(); 

     timer.Tick -= timer_Tick; 
    } 
    private void Frame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e) 
    { 
     if (toast != null) 
     { 

      object layoutRoot = page.FindName("layoutRoot"); 

      if (layoutRoot != null) 
      { 
       ((Panel)layoutRoot).Children.Remove(toast); 

       page = (Windows.UI.Xaml.Controls.Page)e.Content; 

       layoutRoot = page.FindName("layoutRoot"); 

       ((Panel)layoutRoot).VerticalAlignment = VerticalAlignment.Stretch; 

       ((Panel)layoutRoot).Children.Add(toast); 

       if (layoutRoot is Grid) 
       { 
        toast.SetValue(Grid.RowSpanProperty, 100); 
       } 
      } 
     } 
    } 

    public void ShowMessage(string message) 
    { 

     frame = (Windows.UI.Xaml.Controls.Frame)Windows.UI.Xaml.Window.Current.Content; 
     page = (Windows.UI.Xaml.Controls.Page)frame.Content; 

     frame.Navigated -= Frame_Navigated; 
     frame.Navigated += Frame_Navigated; 

     toast = new Ribo.Smart.App.UserControls.Components.Common.Toast(); 
     toast.Message = message; 
     toast.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom; 
     toast.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center; 

     int seconds = message.Length/30; 

     if (seconds < 2) 
      seconds = 2; 

     timer.Interval = new TimeSpan(0, 0, seconds); 
     timer.Start(); 
     timer.Tick -= timer_Tick; 
     timer.Tick += timer_Tick; 

     object layoutRoot = page.FindName("layoutRoot"); 

     if (layoutRoot != null) 
     { 
      if (layoutRoot is Grid) 
      { 
       toast.SetValue(Grid.RowSpanProperty, 100); 
      } 

      ((Panel)layoutRoot).Children.Add(toast); 
     } 

    }