C#?Winform消息通知之系統(tǒng)本地通知local?toast?notification
引言
C#應用通過 Microsoft.Toolkit.Uwp.Notifications
NuGet包可以很方便的發(fā)送本地通知(Windows 10或Windows 11 的 Toast 通知,之前系統(tǒng)版本未提供此類通知),適用于所有類型的應用(WPF、UWP、WinForms、控制臺【實際使用各自有著不同限制】)。
后面會介紹使用任務托盤顯示系統(tǒng)通知,從而不用使用Nuget包。
toast 通知的結構
Toast 內容的核心組件有
- launch:定義當用戶單擊你的 Toast 時將傳回應用的參數(shù),允許深層鏈接到 Toast 所顯示的正確內容。
- visual:toast 的可視部分,包括帶有文本和圖像的通用綁定。
- actions:toast 的交互部分,包括輸入和操作。
- audio:控制向用戶顯示 Toast 時播放的音頻。
Toast 內容的可視化表示形式:
Toast 內容介紹對toast各種應用場景進行了介紹,包括文本、文本行數(shù)、圖片、徽章、時間戳、進度條、按鈕、輸入、音頻等。
發(fā)送本地toast通知的操作步驟
新建項目NotificationLocalToast
。
安裝NuGet包Microsoft.Toolkit.Uwp.Notifications
推薦 7.0 及以上版本。
如果.NET Framework的桌面應用仍使用 packages.config
管理包,需要將其遷移到PackageReference
,否則不會正確引用Windows SDK。
遷移方法是:在項目中,右鍵單擊“引用”,然后單擊“將 packages.config 遷移到 PackageReference”。需要Visual Studio 2017 15.7 及更高版本才支持。
具體參見從 packages.config 遷移到 PackageReference
注:遷移到PackageReference后,需要重啟VS打開項目,否則無法正確的using引用到包庫。
通知的發(fā)送(文本通知)
new ToastContentBuilder() .AddText("CodeMissing發(fā)來一條消息") // 標題文本 .AddText("請檢查消息內容,并及時處理") .Show(); // 7.0以上才提供Show方法
點擊運行并發(fā)送消息,將在屏幕右下角看到toast通知
多次發(fā)送消息,會依照順序各自依次顯示(等待上一個進入操作中心或被關閉)。
處理點擊通知的操作
當用戶點擊通知后,則會在后臺線程調用ToastNotificationManagerCompat.OnActivated
事件,在程序的該事件中處理交互操作。
如果應用程序關閉,將會啟動exe應用(并且 ToastNotificationManagerCompat.WasCurrentProcessToastActivated()
返回true,表示重新啟動了進程),然后再在后臺線程上調用 ToastNotificationManagerCompat.OnActivated 事件。
在主窗體的構造函數(shù)中處理該事件(如果不需要執(zhí)行UI線程的操作,直接在Program.cs的Main函數(shù)入口處添加此監(jiān)聽事件即可)
public MainForm() { InitializeComponent(); // 監(jiān)聽通知激活(點擊) ToastNotificationManagerCompat.OnActivated += toastArgs => { // 通知參數(shù) ToastArguments args = ToastArguments.Parse(toastArgs.Argument); // 獲取任何用戶輸入 ValueSet userInput = toastArgs.UserInput; BeginInvoke(new Action( delegate { // TODO: UI線程的操作 MessageBox.Show("Toast被激活(點擊),參數(shù)是: " + toastArgs.Argument); })); }; }
當點擊通知后,會顯示對話框。
WPF中的(在入口程序中的)異步UI調用要通過Application.Current.Dispatcher.Invoke
,它是位于System.Windows下的Application。需要添加引用PresentationFramework
// Need to dispatch to UI thread if performing UI operations Application.Current.Dispatcher.Invoke(delegate { // TODO: Show the corresponding content MessageBox.Show("Toast activated. Args: " + toastArgs.Argument); });
通知的卸載
如果應用有卸載程序,應該在卸載過程中調用ToastNotificationManagerCompat.Uninstall();
。
如果應用是不安裝的“可移植應用”,則可根據(jù)需要在退出時調用(除非想要在退出后仍然保留通知)。
ToastNotificationManagerCompat.Uninstall()
在執(zhí)行后,會將通知面板中相關的信息清理掉。
卸載方法將清理任何計劃通知和當前通知,刪除任何關聯(lián)的注冊表值,并刪除庫創(chuàng)建的任何關聯(lián)的臨時文件。
設置通知的過期時間
忽略的toast通知會轉到操作中心(通知中心),后續(xù)仍可以查看。
但是,通常消息都有一定的期限,過期后則不應該繼續(xù)顯示或保留,本地 toast 通知的默認和最長過期時間為 3 天。
下面設置過期時間為2天:
new ToastContentBuilder() .Show(toast => { toast.ExpirationTime = DateTime.Now.AddDays(2); });
.NET應用使用Toast Notifications(.NET5+)
.NET中如果使用Microsoft.Toolkit.Uwp.Notifications,必須指定Windows TFM,且要指定window版本,至少為net6.0-windows10.0.17763.0
或更高。否則,將報錯找不到Show()
方法。
- 指定Windows TFM
右鍵項目,編輯項目文件,將TargetFramework
指定為如下:
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
通常,指定TFM后,啟動調試會報錯沒有xxx的目標,確保已運行還原...等錯誤。
解決辦法是:清理項目,并重新生成一次即可。
有時候還會報錯net6.0-windows10.0.17763.0
與.Net
框架版本不一致,需要修改為net5.0-windows10.0.17763.0
?;蛘吒臑閚et5后生成無錯再改回net6;或者直接清理項目并重新生成一次。
添加圖像
使用http圖像
目前僅僅具有Internet功能的UWP/MSIX/sparse應用才支持http的圖像【sparse,松散或稀疏應用】。
UWP/MSIX開發(fā)在Package.appxmanifest
文件中用于指定Internet的能力(默認已經(jīng)開啟)
其他應用,比如Winform、WPF等必須將圖像下載到本地,通過本地路徑引用。
并且web圖片引用有200KB的限制,具體參見官方文檔介紹。不過有個不太清楚的地方:app's package
是什么?又如何從其中獲取圖片?
Images can be used from the app's package, the app's local storage, or from the web. As of the Fall Creators Update, web images can be up to 3 MB on normal connections and 1 MB on metered connections. On devices not yet running the Fall Creators Update, web images must be no larger than 200 KB.
嘗試通過資源pack的方式獲取圖片,未成功,比如WPF圖片中可以使用的"pack://application:,,,/Resources/CSharp.png"
。
內聯(lián)圖像和主圖
下載圖片到本地,并添加到項目中,設置圖片屬性:復制到輸出目錄“總是”或“較新復制”、生成操作“無”,確保圖片生成到exe程序路徑。
首先獲取img文件的完整路徑,并創(chuàng)建Uri,通過AddInlineImage
、AddHeroImage
添加為內聯(lián)圖像和主圖。
- 使用
file:///
構建uri
var imgFileFullPath = Path.GetFullPath("Resources/CSharp.png"); var fileUriString = $"file:///{imgFileFullPath}"; var imgUri = new Uri(fileUriString); new ToastContentBuilder() .AddArgument("action", "viewConversation") // 添加相關參數(shù) .AddArgument("conversationId", 9813) .AddText("CodeMissing發(fā)來一張圖片") // 標題文本 .AddText("這是C#的圖片") .AddInlineImage(imgUri) // 內聯(lián) .AddHeroImage(imgUri) // 主圖 .Show();
注意本地文件的Uri訪問協(xié)議:"file:///FileFullPath"
- 直接使用文件路徑創(chuàng)建Uri
new Uri(localImgPath)
也可以創(chuàng)建uri。
var imgFileFullPath = Path.GetFullPath("Resources/CSharp.png"); new ToastContentBuilder() .AddText("這是C#的圖片") .AddAppLogoOverride(new Uri(imgFileFullPath), ToastGenericAppLogoCrop.Circle) .Show();
徽標和剪裁(圓形圖片)
new ToastContentBuilder() // .... .AddAppLogoOverride(imgUri, ToastGenericAppLogoCrop.Circle) .Show();
UWP使用http圖片
new ToastContentBuilder() .AddArgument("action", "viewConversation") // 添加相關參數(shù) .AddArgument("conversationId", 9813) .AddText("CodeMissing發(fā)來一張圖片") // 標題文本 .AddText("這是C#的圖片") .AddInlineImage(new Uri("https://www.vippng.com/png/detail/398-3984434_c-programming-png.png")) .AddAppLogoOverride(new Uri("https://www.vippng.com/png/detail/398-3984434_c-programming-png.png"), ToastGenericAppLogoCrop.Circle) .Show();
圖片來源于網(wǎng)絡
替換或刪除指定通知
替換或移除toast通知需要使用Tag屬性(以及Group屬性),兩者構成toast的主鍵。
為 toast 設置主鍵
new ToastContentBuilder() .AddText("我是含有Tag和Group的消息") .Show(toast => { toast.Tag = "codemissing101"; toast.Group = "codemissing"; });
根據(jù)Tag和Group刪除或替換toast
通過使用相同的Tag和Group再次發(fā)送消息,即可以實現(xiàn)替換。
new ToastContentBuilder() .AddText("我是替換的消息") .Show(toast => { toast.Tag = "codemissing101"; toast.Group = "codemissing"; });
History.Remove
或者 RemoveGroup
實現(xiàn)移除
ToastNotificationManagerCompat.History.Remove("codemissing101", "codemissing");
如果只有Tag則只指定Tag,如果Tag和Group都存在,則兩者都需要指定才能匹配。
清除通知
ToastNotificationManagerCompat.History.Clear();
參考
以上就是C# Winform消息通知之系統(tǒng)本地通知local toast notification的詳細內容,更多關于C# Winform系統(tǒng)本地消息通知的資料請關注腳本之家其它相關文章!