使用c#在UWP中发送Toast消息的问题

Problem in sending a Toast message in UWP using c#

我正在 UWP 应用程序中使用后台任务发送 toast 消息。代码已成功执行,但未发送 toast 消息。我尝试了很多方法,但其中 none 有效。

这是一个 UWP 应用程序。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;

namespace BackgroundStuff
{
    public sealed class MyBackgroundTask : IBackgroundTask
    {

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            SendToast("Hi this is background Task");
            Debug.WriteLine("Hi this is background Task");
        }

        public static void SendToast(string message)
        {
            //var template = ToastTemplateType.ToastText01;
            //var xml = `ToastNotificationManager.GetTemplateContent(template);`
            //var elements = xml.GetElementsByTagName("text");
            //var text = xml.CreateTextNode(message);

            //elements[0].AppendChild(text);
            //var toast = new ToastNotification(xml);
            //ToastNotificationManager.CreateToastNotifier().Show(toast);

            var xmdock = CreateToast();
            var toast = new ToastNotification(xmdock);
            var notifi = `Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();`
            notifi.Show(toast);
        }

        public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
        {
            var xDoc = new XDocument(
               new XElement("toast",
               new XElement("visual",
               new XElement("binding", new XAttribute("template", `"ToastGeneric"),`
               new XElement("text", "C# Corner"),
               new XElement("text", "Do you got MVP award?")
            )
            ),// actions  
            new XElement("actions",
            new XElement("action", new XAttribute("activationType", `"background"),`
            new XAttribute("content", "Yes"), new XAttribute("arguments", `"yes")),`
            new XElement("action", new XAttribute("activationType", `"background"),`
            new XAttribute("content", "No"), new XAttribute("arguments", `"no"))`
            )
            )
            );

            var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
            xmlDoc.LoadXml(xDoc.ToString());
            return xmlDoc;
        }
    }
}

我期待收到吐司消息,但我没收到。

创建XML文档的过程并没有体现出标签之间的层级关系,所以软件无法将其视为可解析的ToastXML文档。

UWP中的Toast Content基于XML,但是在C#中操作XML不如操作类方便,所以现在有一种新的方式来构建Toast Content .

您可以在您的项目中添加Microsoft.Toolkit.Uwp.Notifications nuget包,然后像这样创建一个toast内容:

var toastContent = new ToastBindingGeneric()
{
    Children =
    {
        new AdaptiveText()
        {
            Text = "Adaptive Tiles Meeting",
            HintMaxLines = 1
        },

        new AdaptiveText()
        {
            Text = "Conf Room 2001 / Building 135"
        },

        new AdaptiveText()
        {
            Text = "10:00 AM - 10:30 AM"
        }
    }
}
var toast = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toast);

这里是 document.

此致。

你可以创建一个通用函数-

using Windows.UI.Notifications;

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