用C#编写Windows10 toast的注意事项。

 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()中。

转载于:https://www.cnblogs.com/jacknb/p/10872633.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值