翻譯|使用教程|編輯:龔雪|2022-05-10 10:14:08.040|閱讀 400 次
概述:本文主要為大家介紹如何使用DevExpress WinForms HTML & CSS模板創(chuàng)建警報(bào)通知,歡迎下載最新版產(chǎn)品體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在最新的DevExpress WinForm版本中,開發(fā)者可以使用WinForm產(chǎn)品線(通過DevExpress AlertControl和ToastNotificationManager)創(chuàng)建兩種類型的通知/警報(bào),最近技術(shù)團(tuán)隊(duì)還推薦使用DevExpress 來(lái)顯示原生 Windows 10+ 通知。
盡管自定義選項(xiàng)有些有限(toast 僅提供九個(gè)布局模板),但ToastNotificationManager 代表了比傳統(tǒng)的基于面板的更好、更現(xiàn)代的替代方案。
在最新版中為AlertControl發(fā)布的HTML & CSS 模板,允許開發(fā)人員創(chuàng)建時(shí)尚的警告通知,同時(shí)可利用AlertControl 本身的簡(jiǎn)單性。下圖說明了技術(shù)團(tuán)隊(duì)提供的一些示例模板(查找您喜歡的模板并將其復(fù)制到項(xiàng)目中):
大多數(shù)通知只是一個(gè)帶有幾個(gè)文本塊、圖像和按鈕的矩形,設(shè)計(jì)這樣簡(jiǎn)單的對(duì)象對(duì)每個(gè)人來(lái)說都應(yīng)該相對(duì)容易——無(wú)論您有 HTML 和 CSS 經(jīng)驗(yàn),還是開始在WinForms 應(yīng)用程序中利用其潛力。 例如以下模板創(chuàng)建一個(gè)帶有圖標(biāo)、標(biāo)題、描述和“確定”按鈕的通知。
<div class="container"> <div class="popup"> <img src="${SvgImage}" class="image" /> <div class="caption">Notification Title</div> <div class="text">This notification uses a web-inspired template.</div> <div id="okButton" class="ok-button">OK</div> </div> </div>
請(qǐng)注意,在此示例標(biāo)記中,通知標(biāo)題和說明是靜態(tài)字符串。 如果您要為用戶顯示一條消息,此解決方案就可以正常工作。
當(dāng)然我們的數(shù)據(jù)綁定功能提供了更大的靈活性——您可以創(chuàng)建一個(gè)模板并將不同的數(shù)據(jù)對(duì)象傳遞給它。 因此,您可以為多個(gè)通知消息重用一個(gè)模板。
如果您更喜歡此選項(xiàng),請(qǐng)使用 ${Data_property_name} 占位符,如下所示:
<div class="text">${Caption}</div> <div class="text">${Text}</div>
“Caption”和“Text”是標(biāo)準(zhǔn)占位符,可以通過 AlertControl.Show 重載直接替換:
alertControl1.Show(this, "Sample caption", "Sample notification text");
您可以添加模板設(shè)計(jì)所需的任意數(shù)量的占位符,但請(qǐng)記住處理 AlertControl.BeforeFormShow 事件并將數(shù)據(jù)對(duì)象傳遞給 e.HtmlPopup.DataContext 屬性。 例如,下面的代碼使用 div 元素來(lái)顯示由五個(gè)占位符組合而成的字符串:兩個(gè)用于字符串值(FullName、Ticker),兩個(gè)用于數(shù)字(Percentage、Price),一個(gè)用于自定義 Direction 枚舉值。
<div class="message-text"> ${FullName} ({Ticker}) {Direction} {Percentage}%. The current price is ${Price}. </div>
通知圖像也在運(yùn)行時(shí)檢索,img 標(biāo)簽使用占位符替代靜態(tài) src 屬性值。
<img src="${StockImage}" class="message-image" />
此示例應(yīng)用程序使用 StockInfo 類對(duì)象作為數(shù)據(jù)項(xiàng)。
public class StockInfo { public StockInfo(string ticker, string fullName, Direction direction, double percentage, double price, SvgImage img) { Ticker = ticker; FullName = fullName; Direction = direction; Percentage = percentage; Price = price; StockImage = img; } public string Ticker { get; set; } public string FullName { get; set; } public Direction Direction { get; set; } public double Percentage { get; set; } public double Price { get; set; } public SvgImage StockImage { get; set; } } public enum Direction { [Description("rises")] Up, [Description("falls")] Down }
當(dāng)數(shù)據(jù)項(xiàng)的 "Price" 值在短時(shí)間內(nèi)發(fā)生顯著變化時(shí)會(huì)觸發(fā)通知,相應(yīng)的項(xiàng)目分配給 AlertControl.BeforeFormShow 事件處理程序中的 e.HtmlPopup.DataContext 屬性。
void AlertControl1_BeforeFormShow(object sender, AlertFormEventArgs e) { // TODO: Retrieve a data item e.HtmlPopup.DataContext = myStockInfoInstance; }
因此,通知會(huì)從指定為 DataContext 的數(shù)據(jù)項(xiàng)中檢索其 ${Field_Name} 占位符的數(shù)據(jù)。 請(qǐng)注意,邊條的顏色會(huì)根據(jù) "Direction" 枚舉值而變化。
DevExpress WinForm擁有180+組件和UI庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)