在我的应用中,我想通知用户何时执行了特定操作,例如成功更新记录或添加新记录,但是没有内置控件可以显示此类信息。有什么类似于UWP的Android Toast.makeText吗?

最佳答案

是的,UWP具有Toast通知:)

这是显示简单通知的示例代码:

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

10-08 15:36