1 using System;2 using System.Windows.Forms;3 using Windows.Data.Xml.Dom;4 using Windows.UI.Notifications;5 6 using System.Collections.Generic;7 using System.Linq;8 using System.Text;9 using System.Ru...

1 using System; 2 using System.Windows.Forms; 3 using Windows.Data.Xml.Dom; 4 using Windows.UI.Notifications; 5 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Runtime.InteropServices; 10 11 namespace ST 12 { 13 public class Toast 14 { 15 public void showTheToast() 16 { 17 string xml = "<toast>" + 18 "<visual>" + 19 "<binding template=\"ToastGeneric\">" + 20 "<text>电池电量不足 " + 21 "</text>" + 22 "<text>请尽快接通电脑电源。</text>" + 23 "</binding>" + 24 "</visual>" + 25 "</toast>"; 26 XmlDocument doc = new XmlDocument(); 27 doc.LoadXml(xml); 28 ToastNotification notification = new ToastNotification(doc); 29 ToastNotifier nt = ToastNotificationManager.CreateToastNotifier(); 30 nt.Show(notification); 31 } 32 33 34 static void Main(string[] args) 35 { 36 Toast toast = new Toast(); 37 toast.showTheToast(); 38 } 39 } 40 }
此代码的作用是产生一个toast,但是生成exe的时候会出错。
System.Exception:“找不到元素。 (异常来自 HRESULT:0x80070490)”
原因在于没有给CreateToastNotifier()传入参数。根据一篇文章的说法,在Windows8之后,程序想要显示toast,必须指定一个appID,这个ID用来指明是什么程序产生的toast。查找这个ID的方法是打开powershell,输入get-StartApps,得到本地的appID列表。选择其中一个,比如说Chrome,代表谷歌浏览器,然后给CreateToastNotifier()传入参数,
ToastNotifier nt = ToastNotificationManager.CreateToastNotifier("Chrome");
就可以生成了,并且产生的toast还会指明是Chrome发出了这个toast。
当你“安装”了一个程序之后,也可以产生一个appID,所以如果你想要用自己的appID,就可以将自己的程序打包成安装程序,安装,将安装后的appID传入CreateToastNotifier()中。
本文标题为:用C#编写Windows10的toast的注意事项。


- user32.dll 函数说明小结 2022-12-26
- Oracle中for循环的使用方法 2023-07-04
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- c# 模拟线性回归的示例 2023-03-14
- Unity Shader实现模糊效果 2023-04-27
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10
- .NET CORE DI 依赖注入 2023-09-27
- Unity3D实现渐变颜色效果 2023-01-16
- 在C# 8中如何使用默认接口方法详解 2023-03-29