如何在UWP App中创建信息丰富的Toast通知

千金一刻莫空度,老大无成空自伤。这篇文章主要讲述如何在UWP App中创建信息丰富的Toast通知相关的知识,希望能为你提供帮助。
在我的应用程序中,我想告知用户特定操作何时执行,例如记录更新成功或添加了新记录,但是没有内置控件可以显示此类信息。是否有类似于UWP的android Toast.makeText?
答案是的,UWP有Toast Notifications :)
以下是显示简单通知的示例代码:

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

【如何在UWP App中创建信息丰富的Toast通知】在本文中,您可以找到如何自定义它:
https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts
另一答案这里如何实现像android这样简单的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); }}

另一答案
# CSharpToast Create a toast in c#This is a Microsoft Visual Studio project that demonstrates showing a toast message to the user.A toast message is one that appears, then after a delay, disappears without user intervention.This is needed when either the default language implementation is lacking, see java Android, or when there is no default language implementation.The steps below illustrate the steps I took to create the toast, but you can, if desired, just download the app here with all of these steps already completed.Usage: Call: Toast.show ("This is a test toast."); DownloadURL: git clone https://github.com/pstorli/CSharpToastSteps To Create: 1) Create a new application in MS Visual Studio. 1.1) File -> New -> Application -> Windows Forms App (.NET Framework) 1.2) Name app as desired, here I used CSharpToast2) Adjust initial form/screen 2.1) Set Form1.cs name to MainWindow.cs 2.4) Set the StartPosition to CenterScreen 2.5) Add a button1 to the form. Set text to "Make Toast" 2.6) Double click button. A new method should appear: private void button1_Click(object sender, EventArgs e) 2.7) Add this code to it: Toast.show ("Toast is done!"); 3) Create the toast form. 3.1) In the solution explorer, Add -> New Item -> Windows Form 3.1.1) Set the name to Toast.cs 3.1.2) Set the toast form width and height to toast size, say 6 inches wide by 1/2" tall. 3.1.3) Set the FormBorderStyle to None 3.1.4) Set the background color to white. 3.1.5) Set the start position to CenterScreen3.2) Add a label to your form 3.2.1) Set the name to Message 3.2.2) Set autosize to false. 3.2.3) Set textalign to MiddleCenter. 3.2.4) Set the background color to white. 3.2.5) Set Dock to fill.3.3) Add some processing logic to file Toast.cs3.3.1) Change Toast.cs from this:using System.Windows.Forms; namespace CSharpToast { public partial class Toast : Form { public Toast() { InitializeComponent(); } } } 3.3.2) to this: 3.3.3) Added DEFAULT_MS_DELAY to control how long, by default toast shoould show up. 3.3.4) Added delegate void SafeOnTimedEvent to call Close from differenet thread. 3.3.5) Added constructor with just message and one with message and delay, to override DEFAULT_MS_DELAY 3.3.6) Created toast and added a timer to call, void OnTimedEvent() , when toast is done which calls Toast.close(); on correct thread.using System; using System.Timers; using System.Windows.Forms; namespace CSharpToast { public partial class Toast : Form {public static int DEFAULT_MS_DELAY = 2500; private delegate void SafeOnTimedEvent(Object source, ElapsedEventArgs e); public Toast (String message) { InitializeComponent(); Message.Text = message; }public static Toast show (String message) { return show(message, DEFAULT_MS_DELAY); }public static Toast show (String message, int ms) { Toast toast = new Toast(message); System.Timers.Timer aTimer = new System.Timers.Timer(ms); aTimer.Elapsed += toast.OnTimedEvent; aTimer.AutoReset = false; aTimer.Enabled = true; toast.ShowDialog(); return toast; }private void OnTimedEvent (Object source, ElapsedEventArgs e) { if (this.InvokeRequired) { var d = new SafeOnTimedEvent(OnTimedEvent); Invoke(d, new object[] { source, e }); } else { Close(); } } } }3.4) Run app 3.4.1) The main screen with the "Make Toast" button should appear. 3.4.2) When you press the "Make Toast" button, 3.4.3) a white toast popup should appear that says "Toast i

    推荐阅读