首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NetCore显示Windows 10通知(敬酒)

.NetCore显示Windows 10通知(敬酒)
EN

Stack Overflow用户
提问于 2020-11-28 21:21:28
回答 1查看 1.6K关注 0票数 1

我已经搜索了许多关于如何从.netCore应用程序创建吐司通知的不同帖子,然而,它们都无助于Microsofts的buggy文档。

因此,在这里得到一个完整的答案,如何显示带有来自.NetCore控制台应用程序的图像的Windows10通知(Toast)?

EN

回答 1

Stack Overflow用户

发布于 2020-11-28 21:21:28

首先,确保您没有针对.NET 5.0 --这个框架还不支持(目前)。

然后,安装Microsoft.Windows.SDK.Contracts NuGet包。

如果您想要显示图标,请使用此选项,并确保您正在设置图像(图标)完整路径,否则只需传递空。

代码语言:javascript
复制
public static void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1)
{

    var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

    var textNodes = template.GetElementsByTagName("text");

    textNodes[0].AppendChild(template.CreateTextNode(h1));
    textNodes[1].AppendChild(template.CreateTextNode(h2));
    textNodes[2].AppendChild(template.CreateTextNode(p1));

    if (File.Exists(imageFullPath))
    {
        XmlNodeList toastImageElements = template.GetElementsByTagName("image");
        ((XmlElement)toastImageElements[0]).SetAttribute("src", imageFullPath);
    }
    IXmlNode toastNode = template.SelectSingleNode("/toast");
    ((XmlElement)toastNode).SetAttribute("duration", "long");

    var notifier = ToastNotificationManager.CreateToastNotifier(appid);
    var notification = new ToastNotification(template);

    notifier.Show(notification);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65054564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档