1. 程式人生 > 實用技巧 >Telerik UI for WinForms - RadTaskDialog控制元件使用方法全解

Telerik UI for WinForms - RadTaskDialog控制元件使用方法全解

Telerik UI for WinForms最新版下載

Telerik UI for WinForms的R3 2020版本中,套件中添加了新的RadTaskDialog元件。 任務對話方塊是一個小視窗,它向用戶顯示資訊並提示他們進行響應。 與常規訊息框相比,任務對話方塊具有許多配置選項,可以顯示其他UI元素(如單選按鈕和進度條),並支援事件處理。

概述

RadTaskDialog是Windows對話方塊和新發布的.NET 5的TaskDialog可替代選擇,該對話方塊是一個視窗,允許使用者執行命令、向用戶提問、為使用者提供資訊或指示進度、正在進行的任務。RadTaskDialog代表標準System.Windows.Forms.MessageBox和RadMessageBox的擴充套件版本,與常規訊息框相比,它可以顯示其他控制元件,例如進度條,並支援事件處理。

功能
  • Auto-Size:視窗的大小基於新增到頁面的內容。
  • 分頁:提供到新頁面的導航(通過從當前屬性重建對話方塊),任務對話方塊可以充當帶有多個頁面的小型嚮導,Microsoft建議使用不超過三頁。
  • 支援的元素:任務對話方塊支援各種預定義的元素(如平面按鈕、常規按鈕、進度條、等待條、單選按鈕、複選框、擴充套件器按鈕、擴充套件器區域、頁尾),這些元素可以分配並自動排列 無需編寫任何佈局邏輯。
  • 圖示:除了表示錯誤、警告、資訊的標準圖示外,任務對話方塊在整個標題/標題背景上還具有綠色、黃色、紅色、灰色或藍色的條形。 此外,開箱即用也支援自定義圖示和影象。
  • 模態與非模態:可以通過ShowDialog或Show方法來模態或非模態顯示。
  • 本地化:每個預定義字串的本地化。
  • 主題:超過25個預定義主題。
  • 自定義:RadTaskDialog可以根據您需要滿足的特定要求進行構造,並允許新增或刪除任何元素以及自定義任何預定義元素。

它有一個包含所有必要使用者資訊的主要元素 - RadTaskDialogPage,RadTaskDialogPage公開了一些有用的屬性,這些屬性使您可以僅用幾行程式碼來設定整個對話方塊:

  • Caption:顯示此頁面時,RadTaskDialogForm標題欄中的文字。
  • Icon:帶有向量影象,並可以顯示綠色、紅色、黃色、藍色或灰色的欄作為標題背景。
  • Heading: 頁面的header/title。
  • Text:顯示有關對話方塊目的的描述性資訊。
  • ProgressBar:用於指示確定或不確定的進度。
  • RadioButtons:單選按鈕集合,允許使用者從不同選項中進行選擇。
  • ContentAreaButtons:顯示在對話方塊頂部的平面按鈕的集合,這些按鈕是扁平的,具有三個主要元素:圖示、標題和描述文字。
  • Expander:定義詳細資訊/描述文字,可以通過切換按鈕將其摺疊。
  • Verification:複選框可用於接收使用者的確認。
  • CommandAreaButtons:顯示在頁面底部的常規按鈕的集合。
  • Footnote: 提供可選的其他說明和幫助,通常針對經驗不足的使用者。
用法

在描述了對話方塊的主要功能之後,就該展示一些用例了。 但是在此之前,我們需要澄清兩件重要的事情:

  • RadTaskDialog需要RadTaskDialogPage作為要顯示的引數。
  • RadTaskDialog不返回System.Windows.Forms.DailogResult(例如MessageBox),而是返回使用者單擊的按鈕的例項。

這是PDF檔案移動期間的示例案例,使用者必須決定是替換原始檔案,取消還是保留兩個檔案。

這是程式碼,大多數行用於配置命令連結按鈕:

RadTaskDialogPage page = new RadTaskDialogPage()
{
SizeToContent = true,
Icon = RadTaskDialogIcon.ShieldBlueBar,
Caption = "Move File",
Heading = "There is already a file with the same name in this location.",
Text = "Click the file you want to keep",
CommandAreaButtons = {
RadTaskDialogButton.Cancel
},
AllowCancel = true,
UseWideContentArea = true
};

RadSvgImage pdfIcon = RadSvgImage.FromFile(@"..\..\Resources\file-pdf.svg");
pdfIcon.Size = new Size(50, 50);
RadTaskDialogCommandLinkButton moveButton = new RadTaskDialogCommandLinkButton(
"Move and Replace",
@"Replace the file in the destination folder with the file you are moving:" + Environment.NewLine +
"document.pdf" + Environment.NewLine + "Size: 275 KB" + Environment.NewLine + "Date Modified: 11.11.2018 12:45");
moveButton.SvgImage = pdfIcon;
page.ContentAreaButtons.Add(moveButton);

RadTaskDialogCommandLinkButton dontMoveButton = new RadTaskDialogCommandLinkButton(
"Don't move",
@"Replace the file in the destination folder with the file you are moving:" + Environment.NewLine +
"document.pdf" + Environment.NewLine + "Size: 275 KB" + Environment.NewLine + "Date Modified: 11.11.2018 12:45");
dontMoveButton.SvgImage = (RadSvgImage)pdfIcon.Clone();
page.ContentAreaButtons.Add(dontMoveButton);

RadTaskDialogCommandLinkButton keepBothButton = new RadTaskDialogCommandLinkButton(
"Move, but keep both files",
"The file you are moving will be renamed 'document(2).pdf'");
page.ContentAreaButtons.Add(keepBothButton);

RadTaskDialogButton clickedButton = RadTaskDialog.ShowDialog(page);
if (clickedButton == null || clickedButton == RadTaskDialogButton.Cancel)
{
// the user cancelled the action
}
else if (clickedButton == moveButton)
{
// move and replace
}
else if (clickedButton == dontMoveButton)
{
// do not move
}
else if (clickedButton == keepBothButton)
{
// move and keep both files
}

另一個有趣的情況是您需要建立一個多頁對話方塊。 要切換頁面,您只需要呼叫當前顯示的RadTaskDialogPage的Navigate方法。

這是印表機安裝示例:

RadTaskDialogButton initialButtonYes = RadTaskDialogButton.Continue;
initialButtonYes.Enabled = false;
initialButtonYes.AllowCloseDialog = false;

RadTaskDialogPage initialPage = new RadTaskDialogPage()
{
Caption = "Hardware Installation",
Heading = "Installation Warning",
Text = "The software you are installing for this hardware:\nPrinters\nhas not passed Windows Logo testing to verify its compatibility with Windows",
Icon = RadTaskDialogIcon.ShieldWarningYellowBar,
AllowCancel = true,

Verification = new RadTaskDialogVerificationCheckBox()
{
Text = "Install anyway"
},

CommandAreaButtons =
{
initialButtonYes,
RadTaskDialogButton.Cancel
},
DefaultButton = RadTaskDialogButton.Cancel
};

RadTaskDialogPage inProgressPage = new RadTaskDialogPage()
{
Caption = "Hardware Installation",
Heading = "Installation in progress...",
Text = "Please wait while the installation is in progress.",
Icon = RadTaskDialogIcon.Information,

ProgressBar = new RadTaskDialogProgressBar()
{
State = RadTaskDialogProgressBarState.Marquee
},

Expander = new RadTaskDialogExpander()
{
Text = "Initializing...",
Position = RadTaskDialogExpanderPosition.AfterFootnote
},

CommandAreaButtons =
{
RadTaskDialogButton.Cancel
}
};

RadTaskDialogPage finishedPage = new RadTaskDialogPage()
{
Caption = "Hardware Installation",
Heading = "Success!",
Text = "The Printer installation completed successfully.",
Icon = RadTaskDialogIcon.ShieldSuccessGreenBar,
CommandAreaButtons =
{
new RadTaskDialogButton("Finish")
}
};

RadTaskDialogVerificationCheckBox checkBox = initialPage.Verification;
checkBox.CheckedChanged += (sender, e) =>
{
initialButtonYes.Enabled = checkBox.Checked;
};

initialButtonYes.Click += (sender, e) =>
{
initialPage.Navigate(inProgressPage);
};

inProgressPage.Created += delegate (object s, EventArgs e)
{
RadTaskDialogProgressBar progressBar = inProgressPage.ProgressBar;
Timer timer = new Timer();
timer.Interval = 2800;
int progressValue = 0;
timer.Start();
timer.Tick += delegate (object sender, EventArgs args)
{
timer.Interval = 40;
if (progressBar.State == RadTaskDialogProgressBarState.Marquee)
{
progressBar.State = RadTaskDialogProgressBarState.Normal;
}

progressBar.Value = progressValue;
inProgressPage.Expander.Text = string.Format("Installation Progress: {0} %", progressValue);
if (progressValue == 100)
{
timer.Stop();
timer.Dispose();
inProgressPage.Navigate(finishedPage);
}

progressValue++;
};
};

最後,技術團隊還元件了三組不同的向量圖示:漸變、平面和白色:

我們添加了三種不同的方法,這些方法根據內部需求返回不同的格式和大小。 要訪問這些影象,可以使用以下程式碼:

// Returns a vector image
RadSvgImage svgIcon = RadTaskDialogIcon.GetSvgImage(RadTaskDialogIconImage.FlatShieldQuestion);

// Returns a raster image with size 16x16px
Image smallIcon = RadTaskDialogIcon.GetSmallImage(RadTaskDialogIconImage.FlatShieldQuestion);

// Returns a raster image with size 26x26px
Image largeIcon = RadTaskDialogIcon.GetLargeImage(RadTaskDialogIconImage.FlatShieldQuestion);


瞭解最新Kendo UI最新資訊,請關注Telerik中文網!