[源碼下載]
html
做者:webabcd
介紹
背水一戰 Windows 10 之 通知(Toast)html5
示例
一、本例用於演示當經過 toast 激活 app 時(前臺方式激活),如何獲取相關信息
Notification/Toast/Demo.xamlc++
<Page x:Class="Windows10.Notification.Toast.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Notification.Toast" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Blue"> <TextBlock Name="lblMsg" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid> </Page>
Notification/Toast/Demo.xaml.csweb
/* * 本例用於演示當經過 toast 激活 app 時(前臺方式激活),如何獲取相關信息 * * * 在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args),以獲取相關的 toast 信息 * * ToastNotificationActivatedEventArgs - 經過 toast 激活應用程序時(前臺方式激活)的事件參數 * Argument - 由 toast 傳遞過來的參數 * UserInput - 由 toast 傳遞過來的輸入框數據 * Kind - 此 app 被激活的類型(ActivationKind 枚舉) * 好比,若是是經過「打開文件」激活的話,則此值爲 File * PreviousExecutionState - 此 app 被激活前的狀態(ApplicationExecutionState 枚舉) * 好比,若是此 app 被激活前就是運行狀態的或,則此值爲 Running * SplashScreen - 獲取此 app 的 SplashScreen 對象 * User - 獲取激活了此 app 的 User 對象 */ using System; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Notification.Toast { public sealed partial class Demo : Page { private ToastNotificationActivatedEventArgs _toastArgs; public Demo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 獲取 ToastNotificationActivatedEventArgs 對象(從 App.xaml.cs 傳來的) _toastArgs = e.Parameter as ToastNotificationActivatedEventArgs; if (_toastArgs != null) { // 獲取 toast 的參數 lblMsg.Text = "argument: " + _toastArgs.Argument; lblMsg.Text += Environment.NewLine; // 獲取 toast 的 輸入框數據 // UserInput 是一個 ValueSet 類型的數據,其繼承自 IEnumerable 接口,能夠 foreach(不能 for) foreach (string key in _toastArgs.UserInput.Keys) { lblMsg.Text += $"key:{key}, value:{_toastArgs.UserInput[key]}"; lblMsg.Text += Environment.NewLine; } } } } }
二、本例用於演示 toast 的基礎
Notification/Toast/Basic.xamlexpress
<Page x:Class="Windows10.Notification.Toast.Basic" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Notification.Toast" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <Button Name="buttonShowToast1" Content="顯示 toast" Click="buttonShowToast1_Click" Margin="5" /> <Button Name="buttonShowToast2" Content="顯示 toast(設置 toast 的過時時間)" Click="buttonShowToast2_Click" Margin="5" /> <Button Name="buttonShowToast3" Content="顯示 toast(無 toast 通知 UI,僅放置於操做中心)" Click="buttonShowToast3_Click" Margin="5" /> </StackPanel> </Grid> </Page>
Notification/Toast/Basic.xaml.csc#
/* * 本例用於演示 toast 的基礎 * 單擊 toast 激活 app 後(前臺方式激活),如何獲取相關信息請參見 Demo.xaml.cs 中的代碼 * * * ToastNotification - toast 通知 * Content - 一個 Windows.Data.Xml.Dom.XmlDocument 類型的對象(在構造函數中須要傳遞此對象),用於描述 toast 的 xml * SuppressPopup - 默認值爲 false * false - 彈出 toast 通知,並放置於操做中心 * true - 不彈出 toast 通知,僅放置於操做中心 * ExpirationTime - 過時時間,超過這個時間就會從操做中心中移除 * ToastNotificationPriority - 優先級(Default 或 High) * Group, Tag - 用於標識 toast 對象(前 16 個字符相同則認爲相同) * 同 group 且同 tag 則視爲同一 toast,即新 toast 會更新舊 toast * 系統會將所用未指定 group 的 toast 視爲同 group * 系統會將所用未指定 tag 的 toast 視爲不一樣 tag * Activated - 經過 toast 激活 app 時觸發的事件 * Dismissed - 彈出的 toast 通知 UI 消失時觸發的事件 * Failed - 彈出 toast 通知時失敗 * * ToastNotificationManager - toast 通知管理器 * CreateToastNotifier() - 建立 ToastNotifier 對象 * History - 獲取 ToastNotificationHistory 對象 * * ToastNotifier - toast 通知器 * Show(ToastNotification notification) - 彈出指定的 toast 通知 * Hide(ToastNotification notification) - 移除指定的 toast 通知 * Setting - 獲取系統的通知設置 * Enabled - 通知可被顯示 * DisabledForApplication - 用戶禁用了此應用程序的通知 * DisabledForUser - 用戶禁用了此計算機此帳戶的全部通知 * DisabledByGroupPolicy - 管理員經過組策略禁止了此計算機上的全部通知 * DisabledByManifest - 應用程序未在 Package.appxmanifest 中設置「應用圖標」(其實你要是不設置的話編譯都不會經過) * * ToastNotificationHistory - 本 app 的 toast 通知歷史 * Clear() - 所有清除(從操做中心中移除) * RemoveGroup() - 清除指定 group 的通知(從操做中心中移除) * Remove() - 清除指定 tag 的通知或清除指定 tag 和 group 的通知(從操做中心中移除) * GetHistory() - 獲取歷史數據,一個 ToastNotification 類型對象的集合(已被從操做中心中移除的是拿不到的) * * * * 注:本例是經過 xml 來構造 toast 的,另外也能夠經過 NuGet 的 Microsoft.Toolkit.Uwp.Notifications 來構造 toast(其用 c# 對 xml 作了封裝) */ using System; using System.Diagnostics; using Windows.Data.Xml.Dom; using Windows.UI.Notifications; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.Notification.Toast { public sealed partial class Basic : Page { public Basic() { this.InitializeComponent(); } // 彈出 toast 通知 private void buttonShowToast1_Click(object sender, RoutedEventArgs e) { // 清除本 app 的以前的所有 toast 通知 // ToastNotificationManager.History.Clear(); // 用於描述 toast 通知的 xml 字符串 string toastXml = $@" <toast activationType='foreground' launch='Notification-Toast-Basic-Arguments 1'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content 1 {DateTime.Now.ToString("mm:ss")}</text> </binding> </visual> </toast>"; // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象 XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); // 實例化 ToastNotification 對象 ToastNotification toast = new ToastNotification(toastDoc); // 系統會將所用未指定 group 的 toast 視爲同 group // 同 group 且同 tag 則視爲同一 toast,即新 toast 會更新舊 toast toast.Tag = "1"; toast.Activated += Toast_Activated; toast.Dismissed += Toast_Dismissed; toast.Failed += Toast_Failed; // 彈出 toast 通知 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.Show(toast); } // 彈出 toast 通知(設置 toast 的過時時間) private void buttonShowToast2_Click(object sender, RoutedEventArgs e) { // 清除本 app 的以前的所有 toast 通知 // ToastNotificationManager.History.Clear(); string toastXml = $@" <toast activationType='foreground' launch='Notification-Toast-Basic-Arguments 2'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content 2 {DateTime.Now.ToString("mm:ss")}</text> </binding> </visual> </toast>"; XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); ToastNotification toast = new ToastNotification(toastDoc); DateTimeOffset expirationTime = DateTimeOffset.UtcNow.AddSeconds(30); toast.ExpirationTime = expirationTime; // 30 秒後 toast 通知將從操做中心中移除 // 系統會將所用未指定 group 的 toast 視爲同 group // 系統會將所用未指定 tag 的 toast 視爲不一樣 tag(此時雖然獲取 tag 時其爲空字符串,可是系統會認爲其是一個隨機值) // 每次彈出此 toast 時都會被認爲是一個全新的 toast // toast.Tag = "2"; toast.Activated += Toast_Activated; toast.Dismissed += Toast_Dismissed; toast.Failed += Toast_Failed; ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.Show(toast); } // 彈出 toast 通知(無 toast 通知 UI,僅放置於操做中心) private void buttonShowToast3_Click(object sender, RoutedEventArgs e) { // 清除本 app 的以前的所有 toast 通知 // ToastNotificationManager.History.Clear(); string toastXml = $@" <toast activationType='foreground' launch='Notification-Toast-Basic-Arguments 3'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content 3 {DateTime.Now.ToString("mm:ss")}</text> </binding> </visual> </toast>"; XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); ToastNotification toast = new ToastNotification(toastDoc); toast.SuppressPopup = true; // 不會彈出 toast 通知 UI,可是會放置於操做中心 toast.Tag = "3"; toast.Activated += Toast_Activated; toast.Dismissed += Toast_Dismissed; toast.Failed += Toast_Failed; ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.Show(toast); } private void Toast_Activated(ToastNotification sender, object args) { Debug.WriteLine(sender.Tag + " Toast_Activated"); } private void Toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args) { Debug.WriteLine(sender.Tag + " Toast_Dismissed"); } private void Toast_Failed(ToastNotification sender, ToastFailedEventArgs args) { Debug.WriteLine(sender.Tag + " Toast_Failed"); } } }
三、演示如何按計劃顯示 toast 通知(在指定的時間顯示指定的 toast 通知)
Notification/Toast/Schedule.xamlwindows
<Page x:Class="Windows10.Notification.Toast.Schedule" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Notification.Toast" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <!--顯示當前 app 的所有 ScheduledToastNotification 對象列表--> <ListBox Name="listBox" Width="800" Height="400" Margin="5" HorizontalAlignment="Left"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Id}" VerticalAlignment="Center" /> <TextBlock Text="{Binding Tag}" Margin="15 0 0 0" VerticalAlignment="Center" /> <HyperlinkButton Name="btnRemoveScheduledToast" Content="刪除此 ScheduledToastNotification" Tag="{Binding Id}" Margin="15 0 0 0" Click="btnRemoveScheduledToast_Click" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button Name="btnAddScheduledToast" Content="添加指定的 ScheduledToastNotification 到計劃列表中" Click="btnAddScheduledToast_Click" Margin="5" /> </StackPanel> </Grid> </Page>
Notification/Toast/Schedule.xaml.csapp
/* * 演示如何按計劃顯示 toast 通知(在指定的時間顯示指定的 toast 通知) * * ScheduledToastNotification - 按計劃顯示 Toast 通知 * Content - Toast 的內容,XmlDocument 類型的數據,只讀,其須要在構造函數中指定 * DeliveryTime - 顯示 Toast 通知的時間,只讀,其須要在構造函數中指定 * SnoozeInterval - 循環顯示 Toast 通知的間隔時長(60 秒 - 60 分之間),只讀,其須要在構造函數中指定(經測試,此邏輯無效) * MaximumSnoozeCount - 循環的最大次數(1 - 5 次),只讀,其須要在構造函數中指定(經測試,此邏輯無效) * Id - ScheduledToastNotification 的標識 * Group, Tag - 用於標識 toast 對象(前 16 個字符相同則認爲相同) * 同 group 且同 tag 則視爲同一 toast,即新 toast 會更新舊 toast * 系統會將所用未指定 group 的 toast 視爲同 group * 系統會將所用未指定 tag 的 toast 視爲不一樣 tag * SuppressPopup - 默認值爲 false * false - 彈出 toast 通知,並放置於操做中心 * true - 不彈出 toast 通知,僅放置於操做中心 * * ToastNotifier - toast 通知器 * AddToSchedule() - 將指定的 ScheduledToastNotification 對象添加到計劃列表 * RemoveFromSchedule() - 從計劃列表中移除指定的 ScheduledToastNotification 對象 * GetScheduledToastNotifications() - 獲取當前 app 的所有 ScheduledToastNotification 對象列表 */ using System; using System.Collections.Generic; using Windows.Data.Xml.Dom; using Windows.UI.Notifications; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Notification.Toast { public sealed partial class Schedule : Page { public Schedule() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { ShowScheduledToasts(); } // 添加指定的 ScheduledToastNotification 到計劃列表中 private void btnAddScheduledToast_Click(object sender, RoutedEventArgs e) { string toastXml = $@" <toast activationType='foreground' launch='Notification-Toast-Schedule-Arguments'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content {DateTime.Now.ToString("mm:ss")}</text> </binding> </visual> </toast>"; // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象 XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); // 實例化 ScheduledToastNotification 對象(15 秒後顯示此 Toast 通知,而後每隔 60 秒再顯示一次 Toast,循環顯示 5 次) // 可是經測試,只會顯示一次,沒法循環顯示,不知道爲何 // ScheduledToastNotification toastNotification = new ScheduledToastNotification(toastDoc, DateTime.Now.AddSeconds(15), TimeSpan.FromSeconds(60), 5); // 實例化 ScheduledToastNotification 對象(15 秒後顯示此 Toast 通知) ScheduledToastNotification toastNotification = new ScheduledToastNotification(toastDoc, DateTime.Now.AddSeconds(15)); toastNotification.Id = new Random().Next(100000, 1000000).ToString(); toastNotification.Tag = toastDoc.GetElementsByTagName("text")[1].InnerText; // 將指定的 ScheduledToastNotification 添加進計劃列表 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.AddToSchedule(toastNotification); ShowScheduledToasts(); } // 刪除指定的 ScheduledToastNotification 對象 private void btnRemoveScheduledToast_Click(object sender, RoutedEventArgs e) { string notificationId = (string)(sender as FrameworkElement).Tag; // 獲取當前 app 的所有 ScheduledToastNotification 對象列表 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); IReadOnlyList<ScheduledToastNotification> notifications = toastNotifier.GetScheduledToastNotifications(); int notificationCount = notifications.Count; for (int i = 0; i < notificationCount; i++) { if (notifications[i].Id == notificationId) { // 從計劃列表中移除指定的 ScheduledToastNotification 對象 toastNotifier.RemoveFromSchedule(notifications[i]); break; } } ShowScheduledToasts(); } // 顯示當前 app 的所有 ScheduledToastNotification 列表 private void ShowScheduledToasts() { // 獲取當前 app 的所有 ScheduledToastNotification 對象列表 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); IReadOnlyList<ScheduledToastNotification> notifications = toastNotifier.GetScheduledToastNotifications(); listBox.ItemsSource = notifications; } } }
OK
[源碼下載]asp.net