"WPF" POPs toast and notification on the WIN10 system

Source: Internet
Author: User
Tags call back

Look at the effect first.

notification in the lower right corner:

Notification of Operation center:

Overall effect :

Prerequisites 1. You need to add a shortcut to the Start menu. 2. Register your COM component that implements the Inotificationactivationcallback interface in the registration table. 3. A app_id, added to the shortcut, Actioncenter to distinguish messages from different applications.

Indispensable, or the notification can not interact with the popup.

Realize

1. Add a shortcut

private void Installshortcut (String shortcutpath, String exepath) {ishelllinkw newshortcut = (Ishelllin KW) 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. IPersistFileNewshortcutsave = (shellhelpers. IPersistFile) Newshortcut;Newshortcutsave. Save(Shortcutpath, True);}

2. Registering COM components

privatevoidRegisterComServer(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);        }

This way actioncenter can find your EXE file by using the GUID.
3. Set the content style of the notification
There are many styles of notifications, and pictures, text, buttons, and input boxes can be used in combination. See the bottom reference link for details.
Here I post the layout settings in my example.

Private voidBtncl (object sender, RoutedEventArgs e) {toastcontent content= NewToastcontent () {Visual= NewToastvisual () {Bindinggeneric= NewToastbindinggeneric () {Children={NewAdaptivetext () {Text="New mirrored Folders Created"//Title},NewAdaptivetext () {Text="Drag some files to either Mirror folder to Sync\nclick to show the Mirror folder on my ..."//Content}}, Applogooverride= NewToastgenericapplogo () {Source= NewSystem.Uri (System.Io.Path.GetFullPath ("123.png")).Absoluteuri//icons for notifications}}}, Scenario=Toastscenario.Alarm,//Set the sound of notifications                //Three buttonActions= NewToastactionscustom () {Buttons={NewToastbutton ("PC",NewQueryString () {{"Action","FileExplorer"},                            {"Path","c:\\"}                        }.ToString ()) {Activationtype=Toastactivationtype.Background},NewToastbutton ("Drive",NewQueryString () {{"Action","FileExplorer"},                            {"Path","d:\\"}                        }.ToString ()) {Activationtype=Toastactivationtype.Background},NewToastbuttondismiss ("Close")                    }                }            }; XmlDocumentXML = NewXmlDocument ();XML.LOADXML (Content.GetContent ()); Toastnotification Toast= NewToastnotification (XML); Toast.Group = "GG";//toast. Expirationtime = DateTime.Now.AddSeconds (a);            //toast. Suppresspopup = true;Toastnotificationmanager.Createtoastnotifier (app_id).Show (toast); }

Two libraries are used here, respectively:

Once the installation is complete, add references.
1. The Querystring.net library inside is a collection of Key-value forms, serialized into a string, because the button in notification only accepts a string of type arguments. When the user clicks on a button, it will call back the Activie method of your COM component, get arguments in this method, and then do the next step.
2. If you don't want the notification to pop up in the bottom right corner, just want the notification to appear in the Action Center (notification bar), you can set a toast. Suppresspopup=true to be shielded. But there is no way to play the sound at this time.
3. On the question of how to make a notification permanent in the "Operations Center", I found it impossible, and Microsoft said, "when the user interacts with the notification, the notification is automatically removed from the Action Center", so that toast. Expirationtime basically has no effect. (See the link below for more information). If scenario = Toastscenario.alarm (Reminder/incomingcall) is set, the user does not click, it will always appear there, otherwise 7-8 seconds after the automatic disappearance.

4. When the program exits, clear the notification
Called on exit:

ToastNotificationManager.History.RemoveGroup(....);//或者ToastNotificationManager.History.Remove(....)

Can. This allows you to delete a notification that belongs to a group, or to delete a tag= "XXX" notification, or a notification under the entire app_id.

5. If there is a problem with the missing DLL during use, add the following reference

The first two references in: C:\Program Files (x86) \reference Assemblies\microsoft\framework.netframework\v4.5\facades\ folder, if your. Net 4.5.2 's framework should be changed to v4.5.2.

The following three references are in the: C:\Windows\System32\WinMetadata\ folder, and the suffix is winmd.

Accessories: Demo. If not, please leave a message or a letter to request [email protected]

If you want more flexibility to control pop-up notifications, you can refer to my blog: "WPF" in the lower right corner of the pop-up notification style (Notification)--A simple tutorial

"Reference link"

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" POPs toast and notification on the WIN10 system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.