DevExpress Winforms使用技巧教程:如何在WinForms Apps中顯示Toast Notifications(二)
下載DevExpress v19.2完整版 DevExpress v19.2漢化資源獲取
DevExpress Winforms Controls 內置140多個UI控件和庫,完美構建流暢、美觀且易于使用的應用程序。使用WinForms PictureEdit控件,可以在表單上顯示圖像。為控件實現DirectX渲染支持,可以輕松處理大的高DPI圖像。想要體驗?點擊下載>>
Activator和COM服務器
Windows在其“操作中心”中保留toast notifications。
通常,操作中心在用戶關閉此屏幕后清除所有toasts(如果用戶打開、關閉并在此打開操作中心,則該操作將為空),但是某些toasts可以保留在操作中心中,直到用戶手動將其關閉為止。即使用戶注銷并返回Windows之后,此類toasts仍保留在操作中心中。此外,單擊toasts會啟動其父應用程序,要顯示此類通知,請創建一個自定義Activator—DevExpress.XtraBars.ToastNotifications.ToastNotificationActivator類的后代。
[Guid("39697E4E-3543-4414-A694-90097B433DC6"), ComVisible(true)] public class ToastNotificationActivatorCustom : ToastNotificationActivator { public override void OnActivate(string arguments, Dictionary<string, string> data){ //specify what happens when a user interacts with a toast } }
您將需要注冊此自定義Activator,在設計時設置組件的ApplicationActivator屬性,或在代碼中調用RegisterApplicationActivator方法。
toastNotificationsManager1.RegisterApplicationActivator( typeof(ToastNotificationActivatorCustom));
您還需要注冊一個COM服務器。 為此,請調用DevExpress.Data.ShellHelper.RegisterComServer方法。 請注意,如果使用ShellHelper類創建應用程序快捷方式,則需要使用帶有activatorType參數的TryCreateShortcut方法重載。 否則,如果您使用應用安裝程序添加應用程序快捷方式,則安裝程序還必須添加HKEY_CURRENT_USER \ SOFTWARE \ Classes \ CLSID \ {-your-GUID-here-} \ LocalServer32注冊表項,并以路徑將應用程序可執行文件添加為 值。
if (!ShellHelper.IsApplicationShortcutExist("My Toast Application") { ShellHelper.TryCreateShortcut( Process.GetCurrentProcess().MainModule.FileName, toastNotificationsManager1.ApplicationId, "My Toast Application", @"D:\Work\Images\_Icons\ico\chain-icon.ico", typeof(ToastNotificationActivatorCustom)); ShellHelper.RegisterComServer( Process.GetCurrentProcess().MainModule.FileName, typeof(ToastNotificationActivatorCustom)); }
GUIDs
使用toasts時,您需要幾個唯一的ID。
- 針對組件的ApplicationID屬性
- 針對每個通知的ID屬性
- 針對標記Activator類的Guid屬性
您可以混搭鍵盤來生成唯一的ID,但是更有效的方法是使用聯機GUID生成器或Visual Studio中包含的生成器("Tools | Create GUID")。
傳遞重要通知
即使您正確設置了所有內容,通知也可能不會傳遞給用戶。 交付會受到以下因素的影響:
- 較舊的OS版本
- 操作中心設置中禁用了通知
- 內部錯誤等
在這種情況下,您可以處理ToastNotificationsManager.Failed事件,以通過其他方法(例如標準消息框)將關鍵消息傳遞給用戶。
using DevExpress.XtraBars.ToastNotifications; using DevExpress.XtraEditors; void manager_Failed(object sender, ToastNotificationFailedEventArgs e) { if ((string)e.NotificationID == "toast_connection_lost_id_das0ud0q94") { IToastNotificationProperties undeliveredToast = toastNotificationsManager1.GetNotificationByID(e.NotificationID); XtraMessageBox.Show(undeliveredToast.Body, undeliveredToast.Header); } }
DevExpress v19.2線上公開課即將開課,前10名免費參與哦~
DevExpress技術交流群:540330292 歡迎一起進群討論
掃描關注DevExpress中文網微信公眾號,及時獲取最新動態及最新資訊
