【wpf】在win10系统上弹出toast和notification
来源:互联网 发布:珠海网络推广发哥 编辑:程序博客网 时间:2024/06/05 07:45
老规矩,先看效果
右下角的notification:
操作中心的notification:
整体效果:
前提条件
1.需要在开始菜单里添加快捷方式。
2.在注册表里注册你实现了INotificationActivationCallBack接口的com组件。
3.一个APP_ID,添加到快捷方式里,ActionCenter会以此来区分不同应用的消息。
缺一不可,不然弹出的notification没法交互。
实现
1.添加相关引用
编辑你项目的csproj文件,添加如下节点
<TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>
然后看起来就是这个样子:
接下来打开引用管理器你会发现左边多了一个Windows选项卡,然后添加图示的三个引用:
然后再添加System.Runtime和System.Runtime.InteropServices.WindowsRuntime引用。
这两个引用在:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\文件夹中,如果你的.net 4.5.2的framework请改为v4.5.2。
2.添加快捷方式
private void InstallShortcut(String shortcutPath, String exePath) { IShellLinkW newShortcut = (IShellLinkW)new CShellLink(); newShortcut.SetPath(exePath); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; PropVariantHelper varAppId = new PropVariantHelper(); varAppId.SetValue(APP_ID); newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ID, varAppId.Propvariant); PropVariantHelper varToastId = new PropVariantHelper(); varToastId.VarType = VarEnum.VT_CLSID; varToastId.SetValue(typeof(NotificationActivator).GUID); newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ToastActivatorCLSID, varToastId.Propvariant); ShellHelpers.IPersistFile newShortcutSave = (ShellHelpers.IPersistFile)newShortcut; newShortcutSave.Save(shortcutPath, true); }
3.注册com组件
private void RegisterComServer(String exePath) { string regString = String.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}\\LocalServer32", typeof(NotificationActivator).GUID); var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(regString); key.SetValue(null, exePath); }
这样ActionCenter就可以通过GUID找到你的exe文件。
4.设置通知的内容样式
通知的样式有很多种,图片、文字、按钮、输入框可以组合使用。详情见最下面的参考链接。
这里我贴出下我例子里的布局设置。
private void btncl(object sender, RoutedEventArgs e) { ToastContent content = new ToastContent() { Visual = new ToastVisual() { BindingGeneric = new ToastBindingGeneric() { Children = { new AdaptiveText() { Text="New Mirrored Folders Created"//标题 }, new AdaptiveText() { Text="Drag some files to either Mirror folder to sync\nClick to show the Mirror folder on my..."//内容 } }, AppLogoOverride = new ToastGenericAppLogo() { Source = new System.Uri(System.IO.Path.GetFullPath("123.png")).AbsoluteUri//通知的图标 } } }, Scenario = ToastScenario.Alarm,//设置通知的声音 //三个button Actions = new ToastActionsCustom() { Buttons = { new ToastButton("PC",new QueryString(){ {"action","fileExplorer" }, {"path","c:\\" } }.ToString()) { ActivationType=ToastActivationType.Background }, new ToastButton("Drive",new QueryString(){ {"action","fileExplorer" }, {"path","d:\\" } }.ToString()) { ActivationType=ToastActivationType.Background }, new ToastButtonDismiss("Close") } } }; XmlDocument xml = new XmlDocument(); xml.LoadXml(content.GetContent()); ToastNotification toast = new ToastNotification(xml); toast.Group = "gg"; //toast.ExpirationTime = DateTime.Now.AddSeconds(20); //toast.SuppressPopup = true; ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast); }
这里用到了两个库,分别是:
安装完成后,添加引用即可。
1. 里面的QueryString.NET库,是将key-value形式的集合,序列化成一个字符串,因为Notification里的button只接受一个为string类型的arguments。用户点击某个button时,会回调你com组件的Activie方法,在这个方法里拿到arguments,然后进行下一步操作。
2. 如果你不想让右下角弹出通知,只想让通知出现在“操作中心”(通知栏)里,可以设置toast.SuppressPopup=true来进行屏蔽。不过此时是没法播放声音的。
3. 关于如何让通知常驻在”操作中心“这个问题,我发现是不可能的,微软说了”当用户与通知进行交互的时候会自动把这条通知从 操作中心移除“,所以那个toast.ExpirationTime基本没啥作用。(详见下面参考链接)。如果设置了Scenario = ToastScenario.Alarm(Reminder/IncomingCall);用户不点击的话,会一直出现在那里,否则7-8秒后自动消失。
5.程序退出时,清除通知
在退出时调用:
ToastNotificationManager.History.RemoveGroup(....);//或者ToastNotificationManager.History.Remove(....)
即可。这样可以删除同属于一个Group的通知,或者删除某个tag=”xxx”的通知,或者整个app_id下的通知。
6.如何做到多个win系统的兼容?
一般你的程序是同时支持win7,win8,win10等的,而win7是无法弹出这种样式的通知的,所以这些引用不能直接添加到主程序里,不然运行时就会报错。
正确的做法就是:将ToastNotification单独做成一个dll,在程序中加入系统版本检测的方法,如果是win10系统,再通过反射的方式,将dll加载到主程序集,然后再弹出通知。
附件:Demo。 如果失效,请留言或来信索取376720513@qq.com
如果你想更灵活的控制弹出的通知,可以参考我这篇博客:【WPF】右下角弹出自定义通知样式(Notification)——简单教程
《参考链接》
1.Quickstart: Handling toast activations from Win32 apps in Windows 10
2.github/desktop-toasts
3.Adaptive and interactive toast notifications
4.Send a local toast notification
- 【wpf】在win10系统上弹出toast和notification
- Toast(弹出信息)和Notification(下拉菜单通知)
- Toast和Notification
- 练习Toast和Notification
- Toast和Notification
- 自定义Notification和Toast
- Android-Toast和Notification
- dialog上弹出toast
- Android中的Toast和Notification
- 在WPF上实现系统消息获
- VS2010开发的WPF程序在XP系统上的运行环境和lognet4在WPF中的配置使用
- android 某些三星手机上不显示通知Notification和消息Toast
- 在Metro App中显示Toast notification
- Toast Notification
- 4 代码引用(notification和Toast)
- 针对于Dialog来说Toast和Notification
- Android自定义Dialog,Toast,Notification和PopupWindow
- Android中的消息通知Toast和Notification
- PHP获取当前位置
- Unity Shader 学习笔记(6) 漫反射
- python UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 15: invalid continuation
- 端口扫描原理
- 解决H3C防火墙双出口做端口映射,数据来回路径不一致问题
- 【wpf】在win10系统上弹出toast和notification
- MySQL安装以及使用教程
- 安卓intent发起广播事件给系统或当前app,并从系统或当前app中接收广播
- spring5.0.1jar包作用
- LKM可加载内核模块简单实例编写
- sqlalchemy 多对多关系
- 接口和抽象类
- 字符串转为小数 保留两位数字
- java中HashMap的使用方法及与Map的关系