2016-05-31 18 views
5

私のアプリでは、レコードが正常に更新されたか新しいレコードが追加されたような、特定のアクションが実行されたときにユーザーに通知したいと考えています。 UWPのAndroid Toast.makeTextに類似したものはありますか?UWPアプリで情報トースト通知を作成する方法

答えて

13

はい、UWPはトースト通知を持っている:)

ここでは、簡単な通知を表示するためのサンプルコードです:あなたはそれをカスタマイズする方法を見つけることができます。この記事では

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); 
} 

を:

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

+0

しかし、私が嫌いな通知センターに入るよりもユーザーがそれを無視した場合は有益な通知ではありません。 –

+0

以下の行を追加してください。 toast.ExpirationTime = DateTime.Now.AddSeconds(4); これは、4秒間の通知を表示し、アクションセンターからも消えます。 –

+0

私は自分の答えを編集しました。 –

0

アンドロイドのような簡単なmakeTextを実現する方法はこちら:

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); 
     } 

    } 
関連する問題