標準DevExpress服務
DevExpress Services將ViewModel中的命令傳遞給View中的控件,這允許您在不分離應用層的情況下修改UI。
可用服務
- MessageBoxService
- DialogService
- Current Dialog Service
- CurrentWindowService
- Window Service
- DocumentManagerService
- WindowedDocumentManagerService
- NavigationService
- DispatcherService
- Notification Service
- SplashScreen Service
- Open and Save File Dialog Services
- Folder Browser Dialog Service
附加信息
- 如何使用服務
- 如何使用服務擴展方法
如何使用服務
1.注冊服務。
- 本地注冊(服務僅在視圖中可用):調用mvvmContext1.RegisterService方法并將 Service 的Create方法之一作為參數傳遞。DevExpress MVVM 框架自動注冊最常用的服務——請參閱下面“全局注冊”部分中的注釋。
- 全局注冊(服務可用于整個應用程序):調用相應的靜態MVVMContext.Register…服務方法。
- 定義一個ViewModel屬性,返回一個相關Service接口的對象(例如,如果注冊了WindowedDocumentManagerService,您的屬性應該是IDocumentManagerService類型)。
- 使用此屬性可訪問服務并調用服務方法向視圖發送命令。
示例
C#:
//1. Global registration MVVMContext.RegisterMessageBoxService(); //1. Local registration mvvmContext1.RegisterService(CreateXtraMessageBoxService()); //2. POCO ViewModel property that returns a Service protected virtual IMessageBoxService MessageBoxService { get { throw new System.NotImplementedException(); } } //3. Send a Service command to a View public void SayHello() { MessageBoxService.Show("Hello!"); }
VB.NET:
'1. Global registration MVVMContext.RegisterMessageBoxService() '1. Local registration mvvmContext1.RegisterService(CreateXtraMessageBoxService()) '2. POCO ViewModel property that returns a Service protected virtual IMessageBoxService MessageBoxService Get Throw New System.NotImplementedException() End Get '3. Send a Service command to a View public void SayHello() MessageBoxService.Show("Hello!")
MessageBoxService
允許您顯示消息框和彈出框。
接口
- IMessageBoxService
管理控件
- System.Windows.Forms.MessageBox
- XtraMessageBox
- FlyoutDialog
Global Registration
C#:
MVVMContext.RegisterMessageBoxService(); MVVMContext.RegisterXtraMessageBoxService(); MVVMContext.RegisterFlyoutMessageBoxService();
VB.NET:
MVVMContext.RegisterMessageBoxService() MVVMContext.RegisterXtraMessageBoxService() MVVMContext.RegisterFlyoutMessageBoxService()
DevExpress MVVM框架自動調用RegisterXtraMessageBoxService方法。
Local Registration
C#:
mvvmContext1.RegisterService( //one of "Create" methods from the list below );
VB.NET:
mvvmContext1.RegisterService( 'one of "Create" methods from the list below )
Create()方法
- Create(DefaultMessageBoxServiceType type) ——使用DefaultMessageBoxServiceType枚舉值來確定要創建的服務類型。
- CreateMessageBoxService() ——創建一個使用標準WinForms消息框的Service。
- CreateXtraMessageBoxService() ——創建一個使用DevExpress XtraMessageBox對象的Service。
- CreateFlyoutMessageBoxService() ——創建一個使用FlyoutDialog對象的服務。
所有四個方法都有對應的重載與第二個IWin32Window所有者參數,此參數允許指定擁有此服務的視圖。如果您傳遞的是null而不是owner參數,框架將嘗試找到一個應該是Service所有者的適當視圖并在大多數情況下使用活動窗口。
Public Service Members
- ShowMessage ——五個顯示消息框的擴展方法。
- MessageBoxFormStyle——允許您訪問消息框表單并修改其外觀設置。例如,下面的代碼說明了如何將粗體字體樣式應用于消息框按鈕。
C#:
var msgService = MessageBoxService.CreateFlyoutMessageBoxService(); msgService.MessageBoxFormStyle = (form) => { { FlyoutDialog msgFrm = form as FlyoutDialog; msgFrm.Properties.AppearanceButtons.FontStyleDelta = FontStyle.Bold; };
VB.NET:
Dim msgService = MessageBoxService.CreateFlyoutMessageBoxService(Me) msgService.DialogFormStyle = Sub(form) Dim msgFrm As FlyoutDialog = TryCast(form, FlyoutDialog) msgFrm.Properties.AppearanceButtons.FontStyleDelta = FontStyle.Bold End Sub
DialogService
允許您顯示對話框。
接口
IDialogService
管理控件
- XtraForm
- FlyoutDialog
- RibbonForm
Global Registration
C#:
MVVMContext.RegisterXtraDialogService(); MVVMContext.RegisterFlyoutDialogService(); MVVMContext.RegisterRibbonDialogService();
VB.NET:
MVVMContext.RegisterXtraDialogService() MVVMContext.RegisterFlyoutDialogService() MVVMContext.RegisterRibbonDialogService()
DevExpress MVVM框架自動調用RegisterXtraDialogService方法。
Local Registration
C#:
mvvmContext1.RegisterService(DialogService.CreateXtraDialogService(this)); mvvmContext1.RegisterService(DialogService.CreateFlyoutDialogService(this)); mvvmContext1.RegisterService(DialogService.CreateRibbonDialogService(this)); mvvmContext1.RegisterService(DialogService.Create(this, DefaultDialogServiceType.RibbonDialog));
VB.NET:
mvvmContext1.RegisterService(DialogService.CreateXtraDialogService(Me)) mvvmContext1.RegisterService(DialogService.CreateFlyoutDialogService(Me)) mvvmContext1.RegisterService(DialogService.CreateRibbonDialogService(Me)) mvvmContext1.RegisterService(DialogService.Create(Me, DefaultDialogServiceType.RibbonDialog))
Create()方法
DialogService的所有' Create…'方法都需要一個擁有該服務的視圖。如果傳遞的是null而不是View,框架會嘗試找到一個合適的窗口(在大多數情況下,會使用活動窗口)。
- Create(IWin32Window owner, DefaultDialogServiceType type) ——使用DefaultDialogServiceType枚舉值來確定要創建的服務類型。
- CreateXtraDialogService(IWin32Window所有者)——創建一個顯示可剝皮DevExpress對話框的服務。
- CreateFlyoutDialogService(IWin32Window所有者)——創建一個顯示flyoutdialog的服務。
- CreateRibbonDialogService(IWin32Windowowner)——創建一個服務,將帶有嵌入式RibbonControl的RibbonForm顯示為對話框,對話框按鈕顯示為功能區項目。
- Create(IWin32Window owner, string title, Func<IDialogForm> factoryMethod)——允許您注冊一個Service來管理自定義對話框(實現IDialogForm接口的對象)。
C#:
DialogService.Create(ownerView1, "A custom dialog", ()=> new CustomDialogClass());
VB.NET:
DialogService.Create(ownerView1, "A custom dialog", Function() New CustomDialogClass())
- DialogService Create(IWin32Windowowner, string title, IDialogFormFactoryfactory)——接受創建自定義對話框的工廠類。
Public Service Methods
ShowDialog——六種擴展方法,顯示具有特定外觀和內容的對話框。
C#:
public void FindCustomer() { if(DialogService.ShowDialog(MessageButton.OKCancel, "Find Customer", findDialogViewModel) == MessageResult.OK) { // do something } }
VB.NET:
Public Sub FindCustomer() If DialogService.ShowDialog(MessageButton.OKCancel, "Find Customer", findDialogViewModel) = MessageResult.OK Then ' do something End If End Sub
這些重載允許您用自定義UICommand對象替換默認對話框按鈕。為此,使用自定義命令的Id或Tag屬性作為MessageResult或DialogResult值。
C#:
public void FindCustomer() { var findDialogViewModel = FindDialogViewModel.Create(); findDialogViewModel.SetParentViewModel(this); var commands = new List<UICommand> { // Button with custom command attached new UICommand { Id = "Find", Caption = "Find", Command = new DelegateCommand(() =>{ // . . . implement the Find command here }), IsDefault = true, IsCancel = false, Tag = DialogResult.OK }, // standard button caption customization new UICommand { Caption = "Cancel Find", Tag = DialogResult.Cancel } }; DialogService.ShowDialog(commands, "Find Customer", "FindDialogView", SelectedEntity, findDialogViewModel); }
VB.NET:
Public Sub FindCustomer() Dim findDialogViewModel = FindDialogViewModel.Create() findDialogViewModel.SetParentViewModel(Me) Dim commands = New List(Of UICommand) From {New UICommand With {.Id = "Find", .Caption = "Find", .Command = New DelegateCommand(Sub() End Sub), .IsDefault = True, .IsCancel = False, .Tag = DialogResult.OK }, New UICommand With {.Caption = "Cancel Find", .Tag = DialogResult.Cancel} } DialogService.ShowDialog(commands, "Find Customer", "FindDialogView", SelectedEntity, findDialogViewModel) End Sub
DialogFormStyle——允許您訪問對話框并修改其外觀設置。例如,下面的代碼說明了如何將粗體字體樣式應用于彈出對話框按鈕。
C#:
var service = DialogService.CreateFlyoutDialogService(this); service.DialogFormStyle = (form) => { FlyoutDialog dialog = form as FlyoutDialog; dialog.Properties.AppearanceButtons.FontStyleDelta = FontStyle.Bold; };
VB.NET:
Dim service = DialogService.CreateFlyoutDialogService(Me) service.DialogFormStyle = Sub(form) Dim dialog As FlyoutDialog = TryCast(form, FlyoutDialog) dialog.Properties.AppearanceButtons.FontStyleDelta = FontStyle.Bold End Sub
當前對話服務
允許您管理當前可見的對話框。
接口
DevExpress.Mvvm.ICurrentDialogService
注冊
服務只有在有活動對話框時才存在——您不能注冊CurrentDialogService。
Create()方法
沒有
Public Service Methods
- Close()、Close(MessageResultdialogResult)和Close (UICommanddialogResult) —— 使用給定的DialogResult關閉對話框,如果結果是UICommand類型,則調用相關的UICommand 。請注意,您只能使用最初傳遞到該方法中的對話框服務的UICommand之一ShowDialog。
- WindowState——這個屬性允許您改變對話框的窗口狀態(正常,最小化或最大化)。
當前窗口服務
類似于CurrentDialogService,但是允許您管理當前的應用程序窗口(形式)。
接口
DevExpress.Mvvm.ICurrentWindowService
Global Registration
不可用。
Local Registration
C#:
mvvmContext1.RegisterService(CurrentWindowService.Create(this)); mvvmContext1.RegisterService(CurrentWindowService.Create(listBoxControl1));
VB.NET:
mvvmContext1.RegisterService(CurrentWindowService.Create(Me)) mvvmContext1.RegisterService(CurrentWindowService.Create(listBoxControl1))
Create()方法
- Create(控制容器)——允許您為任何承載作為方法參數分配的控件的表單注冊服務。
- 創建(Form currentForm)——為這個表單注冊一個服務。
- Create(Func<Form> getCurrentForm)——為getCurrentForm方法返回的任何表單注冊一個Service。
公共服務API
Activate()、Close()、Hide()和Show() ——允許您控制當前窗口的可見性。
WindowState ——此屬性允許您更改窗體的窗口狀態(正常、最小化或最大化)。
窗口服務
允許您將視圖顯示為獨立的窗口(形式),并從ViewModel層管理這些窗口。
接口
IWindowService
管理控件
- XtraForm
- RibbonForm
- FlyoutPanel
Global Registration
C#:
MVVMContext.RegisterXtraFormService(); MVVMContext.RegisterFlyoutWindowService(); MVVMContext.RegisterRibbonWindowService();
VB.NET:
MVVMContext.RegisterXtraFormService() MVVMContext.RegisterFlyoutWindowService() MVVMContext.RegisterRibbonWindowService()
Local Registration
C#:
mvvmContext1.RegisterService(WindowService.Create(this, DefaultWindowServiceType.RibbonForm, "Window Title")); mvvmContext1.RegisterService(WindowService.CreateXtraFormService(this, "Window Title")); mvvmContext1.RegisterService(WindowService.CreateRibbonWindowService(this, "Window Title")); mvvmContext1.RegisterService(WindowService.CreateFlyoutWindowService(this, "Window Title"));
VB.NET:
mvvmContext1.RegisterService(WindowService.Create(Me, DefaultWindowServiceType.RibbonForm, "Window Title")) mvvmContext1.RegisterService(WindowService.CreateXtraFormService(Me, "Window Title")) mvvmContext1.RegisterService(WindowService.CreateRibbonWindowService(Me, "Window Title")) mvvmContext1.RegisterService(WindowService.CreateFlyoutWindowService(Me, "Window Title"))
本地注冊(模態窗口)
如果您想把表單顯示為模態對話框請在注冊前修改Service的ShowMode屬性。
C#:
var service = WindowService.CreateXtraFormService(this, "Window Title"); service.ShowMode = WindowService.WindowShowMode.Modal; mvvmContext1.RegisterService(service);
VB.NET:
Dim service = WindowService.CreateXtraFormService(Me, "Window Title") service.ShowMode = WindowService.WindowShowMode.Modal mvvmContext1.RegisterService(service)
Create()方法
CreateXtraFormService(IWin32Window owner, string title = null)——創建一個管理xtraform的服務。
CreateRibbonWindowService(IWin32Window owner, string title = null)——創建一個管理Ribbon窗體的服務。
CreateFlyoutWindowService(IWin32Window owner, string title = null)——創建一個管理Flyouts的服務。
Create(IWin32Window owner, DefaultWindowServiceType type, string title = null)——創建一個Service,其類型取決于type參數。
Create(IWin32Window owner, string title = null, Func<IWindow> factoryMethod = null) ——允許注冊一個服務來管理自定義表單(實現IWindowFactory接口的對象)。
Create(IWin32Window owner, string title = null, IWindowFactory factory = null)——接受一個創建自定義窗口的工廠類。
公共服務方式
- Show(object viewModel)——顯示與此 ViewModel 關聯的視圖。
- Show(string documentType, object viewModel)——顯示由目標 ViewModel 管理的特定視圖。
- Show(string documentType, objectparameter, objectparentViewModel)——允許您將特定參數傳遞到表單。
- Hide()和Activate()——允許您最小化表單或將其置于最前面。
- Close()——關閉窗口管理。
DocumentManagerService
提供在MDI(多文檔接口)控件中創建和管理選項卡的方法的本地服務。
接口
IDocumentManagerService
管理控件
- DocumentManager
- Navigation Frame
- XtraTabControl
- XtraTabbedMdiManager
- Dock Manager
- TabFormControl
Global Registration
由于該服務管理特定的內容提供程序,因此您無法全局注冊該服務。
Local Registration
C#:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1))
Create()方法
- Create(IDocumentAdapterFactory factory)——創建一個控制特定提供者的服務,提供程序是類的控件或對象,派生自IDocumentAdapterFactory接口。factory參數接受以下類型的對象:
- 所有
- 選項卡MDI管理器
- XtraTabControl
- 導航框架
- Dock Manager
- TabFormControl
- Create(Func<IDocumentAdapter> factoryMethod)——接受一個初始化新工廠對象的factoryMethod函數,這允許您創建自定義工廠(實現IDocumentAdapterFactory接口的對象)。
Global Registration
由于該服務管理特定的內容提供程序,因此您無法全局注冊該服務。
Local Registration
C#:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1))
Create()方法
- Create(IDocumentAdapterFactory factory)——創建一個控制特定提供者的服務,提供程序是類的控件或對象,派生自IDocumentAdapterFactory接口。factory參數接受以下類型的對象:
- 所有DocumentManager視圖
- 選項卡MDI管理器
- XtraTabControl
- 導航框架
- Dock Manager
- TabFormControl
- Create(Func<IDocumentAdapter> factoryMethod)——接受一個初始化新工廠對象的factoryMethod函數,這允許您創建自定義工廠(實現IDocumentAdapterFactory接口的對象)。
公共服務方式
- Documents——提供對托管內容提供者擁有的項(文檔、選項卡、頁面)集合的訪問的屬性。
- ActiveDocument——獲得或設置一個活躍的項目。
- CreateDocument——創建該內容提供商擁有的新項目的三種擴展方法。創建的項目的類型取決于提供者類型。對于TabbedView、NativeMdiView視圖和XtraTabbedMdiManager控件,CreateDocument方法創建一個項目,作為選項卡??康教峁┏绦?。為了創建浮動項,請改用 WindowedDocumentManagerService (見下文)。
窗口文檔管理器服務
允許您添加承載自定義內容的新表單。如果服務是用Create(IDocumentAdapterFactory factory)方法注冊的,它會添加新的浮動DocumentManager/XtraTabbedMdiManager面板而不是表單。
接口
IDocumentManagerService
管理控件
- System.Windows.Forms.Form
- XtraForm
- RibbonForm
- FlyoutDialog
Global Registration
C#:
MVVMContext.RegisterFormWindowedDocumentManagerService(); MVVMContext.RegisterXtraFormWindowedDocumentManagerService(); MVVMContext.RegisterRibbonFormWindowedDocumentManagerService();
VB.NET:
MVVMContext.RegisterFormWindowedDocumentManagerService() MVVMContext.RegisterXtraFormWindowedDocumentManagerService() MVVMContext.RegisterRibbonFormWindowedDocumentManagerService()
DevExpress MVVM框架自動調用XtraFormWindowedDocumentManagerService方法。
Local Registration
C#:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(this)); mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateXtraFormService()); mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateRibbbonFormService()); mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateFlyoutFormService()); mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(this, DefaultWindowedDocumentManagerServiceType.RibbonForm)); mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(Me)) mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateXtraFormService()) mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateRibbbonFormService()) mvvmContext1.RegisterService(WindowedDocumentManagerService.CreateFlyoutFormService()) mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(Me, DefaultWindowedDocumentManagerServiceType.RibbonForm)) mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1))
Create()方法
如果您傳遞的是null而不是owner參數,框架會嘗試找到一個應該是Service所有者的視圖,在大多數情況下,使用活動窗口。
- Create(IWin32Window owner)——創建具有特定所有者的默認類型的Service,默認類型是全局注冊的類型。例如,如果您有全局注冊的功能區表單服務(RegisterRibbonFormWindowedDocumentManagerService),本地服務也會顯示功能區表單,如果沒有注冊全局服務,則默認類型為XtraForm。
- Create(IWin32Window owner, DefaultWindowedDocumentManagerServiceType type)——創建一個具有目標所有者的本地服務,服務類型取決于類型參數。
- CreateXtraFormService(IWin32Window owner)——注冊一個服務,在XtraForms中托管它的項目。
- CreateRibbbonFormService(IWin32Window owner) ——注冊一個服務,在RibbonForms中托管它的項目。
- CreateFlyoutFormService(IWin32Window owner)——注冊一個服務,該服務在彈出對話框中承載其項目。
- Create(IDocumentAdapterFactory factory) —— 一種擴展方法,允許您為 WindowedDocumentManagerService設置本地內容提供程序,使用此方法注冊的服務將子提供程序項目添加為浮動表單。例如,以下代碼注冊與DocumentManager的TabbedView關聯的服務,當您調用該CreateDocument方法時,服務會將浮動文檔添加到此TabbedView。
C#:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1))
下面的對象實現了IDocumentAdapterFactory接口,并且可以作為參數傳遞給這個方法:
- DocumentManager組件的TabbedView和NativeMdiView視圖
- XtraTabbedMdiManager
XtraTabControl和NavigationFrame子項目總是停靠的,不能將這些控件用作工廠參數。
Create(Func<Form> factoryMethod, IWin32Window owner) ——允許您創建自定義工廠(實現IDocumentAdapterFactory接口的對象)。
公共服務方式
- Documents——提供對此服務管理的項集合的訪問的屬性。
- ActiveDocument——獲取或設置活動項。
- CreateDocument——創建新項的三個擴展方法,根據注冊的不同,項目是一個獨立的表單/XtraForm/RibbonForm或浮動面板由DocumentManager/XtraTabbedMdiManager擁有。
導航服務
該服務允許您在NavigationFrame控件中從一個視圖導航到另一個視圖,并將應用程序視圖作為托管控件中的頁面打開(例如,作為TabbedView選項卡)。
接口
INavigationService
管理控件
- 導航框架
- DocumentManager
- XtraTabControl
- XtraTabbedMdiManager
- Dock Manager
- TabFormControl
Global Registration
不可用。
Local Registration
C#:
mvvmContext1.RegisterService(NavigationService.Create(navigationFrame1));
VB.NET:
mvvmContext1.RegisterService(NavigationService.Create(navigationFrame1))
Create()方法
Create(IDocumentAdapterFactory factory)——允許您為此服務設置本地內容提供者的擴展方法,當使用此方法創建時,服務將創建新項作為提供者的子項。
公共服務方式
與DocumentManagerService中相同的命令可用,加上以下導航API:
- BackNavigationMode——允許您指定當用戶按下“返回”按鈕時屏幕上出現的模塊:前一個模塊還是根模塊。
- GoBack, GoForward ——導航到先前查看的模塊或放棄此導航。
- CanGoBack, CanGoForward ——返回是否可以在給定方向上導航。
- Navigate ——導航到目標視圖,其名稱作為字符串參數傳遞給此方法。
DispatcherService
允許您使用dispatcher在ViewModel中執行操作。
接口
管理控件
沒有。
Global Registration
此服務已注冊。
Local Registration
C#:
mvvmContext1.RegisterService(DispatcherService.Create());
VB.NET:
mvvmContext1.RegisterService(DispatcherService.Create())
Create()方法
- Create()——創建一個新的Service實例。
公共服務方式
BeginInvoke——異步執行指定的委托。
C#:
async Task DoSomethingAsync(){ var dispatcher = this.GetService<IDispatcherService>(); // Obtain the UI-thread's dispatcher // Do something asynchronously await Task.Delay(100); await dispatcher.BeginInvoke(()=>{ // Perform an update // this.RaisePropertiesChanged() }); }
VB.NET:
Private Async Sub DoSomethingAsync() As Task Dim dispatcher = Me.GetService(Of IDispatcherService)() 'Obtain the UI-thread's dispatcher ' Do something asynchronously Await Task.Delay(100) Await dispatcher.BeginInvoke(Function() ' Perform an update ' Me.RaisePropertiesChanged() End Function) End Sub
通知服務
顯示傳統的警報窗口和Windows Toast通知。
接口
INotificationService
管理控件
- Toast Notification Manager
- Alert Windows
Global Registration
不可用。
Local Registration
C#:
mvvmContext.RegisterService(NotificationService.Create(toastNotificationManager));
VB.NET:
mvvmContext.RegisterService(NotificationService.Create(toastNotificationManager))
Create()方法
- Create(INotificationProvider manager)——創建一個使用目標管理器顯示通知的服務,接受ToastNotificationsManager和AlertControl類實例作為參數。
公共服務方式
- CreatePredefinedNotification(string header, string body, string body2, object image = null)——創建帶有圖像、標題文本字符串和兩個常規正文文本字符串的通知。注意,這個方法創建了一個通知,但沒有顯示它——要使它可見,請調用ShowAsync方法。請參閱下面的代碼片段來獲取示例。
C#:
protected INotificationService INotificationService { get { return this.GetService<INotificationService>(); } } public virtual INotification Notification { get; set; } public async void ShowNotification() { // Create a notification with the predefined template. Notification = INotificationService.CreatePredefinedNotification("Hello", "Have a nice day!", "Greeting"); // Display the created notification asynchronously. try { await Notification.ShowAsync(); } catch(AggregateException e) { // Handle errors. MessageBoxService.ShowMessage(e.InnerException.Message, e.Message); } } public void HideNotification() { // Hide the notification Notification.Hide(); }
VB.NET:
Protected ReadOnly Property INotificationService() As INotificationService Get Return Me.GetService(Of INotificationService)() End Get End Property Public Overridable Property Notification() As INotification Public Async Sub ShowNotification() ' Create a notification with the predefined template. Notification = INotificationService.CreatePredefinedNotification("Hello", "Have a nice day!", "Greeting") ' Display the created notification asynchronously. Try Await Notification.ShowAsync() Catch ex As AggregateException ' Handle errors. MessageBoxService.ShowMessage(ex.InnerException.Message, ex.Message) End Try End Sub Public Sub HideNotification() ' Hide the notification. Notification.Hide() End Sub
如果該ShowAsync方法無法顯示通知(例如,如果 Windows 操作系統設置禁用 toast 通知),則該方法會在非UI線程中異步引發異常,此異常不會影響UI線程。要處理這些異常并響應通知顯示失敗,請ShowAsync使用塊包裝方法的調用try..catch。
- CreateCustomNotification(object viewModel)——創建一個帶有 ViewModel 的通知,ViewModel 參數需要一個實現DevExpress.Utils.MVVM.Services.INotificationInfo接口的類的實例。該界面公開一張圖像和三個字符串屬性,允許您通知設置圖標、標題文本字符串和兩個常規文本字符串。下面的代碼說明了一個示例。
C#:
public class HelloViewModelWithINotificationInfo : INotificationInfo { protected INotificationService INotificationService { get { return this.GetService<INotificationService>(); } } public virtual INotification Notification { get; set; } public void ShowNotification() { // Creating a custom notification Notification = INotificationService.CreateCustomNotification(this); } string INotificationInfo.Header { get { return "Hello, buddy!"; } } string INotificationInfo.Body { get { return "Have a nice day!"; } } string INotificationInfo.Body2 { get { return "Greeting"; } } System.Drawing.Image INotificationInfo.Image { get { return null; } } }
VB.NET:
Public Class HelloViewModelWithINotificationInfo Implements INotificationInfo Protected ReadOnly Property INotificationService() As INotificationService Get Return Me.GetService(Of INotificationService)() End Get End Property Public Overridable Property Notification() As INotification Public Sub ShowNotification() ' Creating a custom notification Notification = INotificationService.CreateCustomNotification(Me) End Sub Private ReadOnly Property INotificationInfo_Header() As String Implements INotificationInfo.Header Get Return "Hello, buddy!" End Get End Property Private ReadOnly Property INotificationInfo_Body() As String Implements INotificationInfo.Body Get Return "Have a nice day!" End Get End Property Private ReadOnly Property INotificationInfo_Body2() As String Implements INotificationInfo.Body2 Get Return "Greeting" End Get End Property Private ReadOnly Property INotificationInfo_Image() As System.Drawing.Image Implements INotificationInfo.Image Get Return Nothing End Get End Property End Class
CreateCustomNotification方法創建一個通知,但不顯示它,要顯示通知,調用通知的' show '和' Hide '方法。
啟動畫面服務
此服務允許您顯示啟動屏幕和等待表單,表明應用程序正忙。
接口
管理控件
Splash Screen Manager
Global Registration
此服務已注冊。
Local Registration
C#:
mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager));
VB.NET:
mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager))
Create()方法
- Create(ISplashScreenServiceProvider serviceProvider)——創建一個管理目標啟動屏幕管理器的服務。
- Create(ISplashScreenServiceProvider serviceProvider, DefaultBoolean throwExceptions) ——創建一個服務,該服務管理目標啟動屏幕管理器,并在發生錯誤時拋出異常。
公共服務方式
ShowSplashScreen(string documentType)—— 顯示啟動屏幕或特定類型的等待表單。“documentType”參數是從SplashScreen類派生的 ViewModel 的名稱,表示需要顯示的啟動屏幕。如果傳遞null作為參數,則會創建由DevExpress設計的默認啟動屏幕。
要顯示Fluent Splash Screen或Overlay Form,請將相應的字符串 ID 傳遞給該ShowSplashScreen方法。
疊加形式:
C#:
//ViewModel public class OverlayViewModel { protected ISplashScreenService SplashScreenService { get { return this.GetService<ISplashScreenService>(); } } public async System.Threading.Tasks.Task Wait() { SplashScreenService.ShowSplashScreen("#Overlay#"); //do something await System.Threading.Tasks.Task.Delay(2500); SplashScreenService.HideSplashScreen(); } } //View mvvmContext.ViewModelType = typeof(OverlayViewModel); mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager)); var fluent = mvvmContext.OfType<OverlayViewModel>(); fluent.BindCommand(showButton, x => x.Wait);
VB.NET:
'ViewModel Public Class OverlayViewModel Protected ReadOnly Property SplashScreenService() As ISplashScreenService Get Return Me.GetService(Of ISplashScreenService)() End Get End Property Public Async Function Wait() As System.Threading.Tasks.Task SplashScreenService.ShowSplashScreen("#Overlay#") 'do something Await System.Threading.Tasks.Task.Delay(2500) SplashScreenService.HideSplashScreen() End Function End Class 'View mvvmContext.ViewModelType = GetType(OverlayViewModel) mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager)) Dim fluent = mvvmContext.OfType(Of OverlayViewModel)() fluent.BindCommand(showButton, Function(x) x.Wait)
流暢的啟動界面:
C#:
//ViewModel public class FluentSplashScreenViewModel { protected ISplashScreenService SplashScreenService { get { return this.GetService<ISplashScreenService>(); } } public void Show() { SplashScreenService.ShowSplashScreen("#FluentSplashScreen#"); } public void Hide() { System.Threading.Thread.Sleep(1000); SplashScreenService.HideSplashScreen(); } } //View mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager)); var fluent = mvvmContext.OfType<FluentSplashScreenViewModel>(); fluent.BindCommand(showButton, x => x.Show); fluent.BindCommand(hideButton, x => x.Hide);
VB.NET:
'ViewModel Public Class FluentSplashScreenViewModel Protected ReadOnly Property SplashScreenService() As ISplashScreenService Get Return Me.GetService(Of ISplashScreenService)() End Get End Property Public Sub Show() SplashScreenService.ShowSplashScreen("#FluentSplashScreen#") End Sub Public Sub Hide() System.Threading.Thread.Sleep(1000) SplashScreenService.HideSplashScreen() End Sub End Class 'View mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager)) Dim fluent = mvvmContext.OfType(Of FluentSplashScreenViewModel)() fluent.BindCommand(showButton, Function(x) x.Show) fluent.BindCommand(hideButton, Function(x) x.Hide)
- HideSplashScreen()——隱藏活動的啟動屏幕或等待表單。
- SetSplashScreenProgress(double progress, double maxProgress) and SetSplashScreenState(object state) ——將自定義數據注入當前可見的啟動畫面或等待表單的方法,SetSplashScreenProgress方法更新啟動屏幕進度條,SetSplashScreenState發送任何其他類型的數據(例如,啟動屏幕標簽的字符串數據)。
啟動畫面
啟動畫面可以利用這兩種方法,要接收和使用注入的數據,請使用啟動屏幕管理器的智能標簽菜單添加新的啟動屏幕。啟動屏幕的代碼包含“覆蓋”區域:覆蓋其SplashFormBase.ProcessCommand方法來解析數據。
C#:
public partial class SplashScreen1 : SplashScreen { public SplashScreen1() { InitializeComponent(); } #region Overrides public override void ProcessCommand(Enum cmd, object arg) { base.ProcessCommand(cmd, arg); } #endregion }
VB.NET:
Partial Public Class SplashScreen1 Inherits SplashScreen Public Sub New() InitializeComponent() End Sub #Region "Overrides" Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object) MyBase.ProcessCommand(cmd, arg) End Sub #End Region End Class
SetSplashScreenProgress 和SetSplashScreenState方法還可以將數據發送到啟動屏幕和等待表單。為此,請使用簡單對象(字符串、數值等)作為方法參數,執行此操作時,SplashFormBase.ProcessCommand方法將接收這些簡單對象作為arg參數,并接收DemoProgressSplashScreen.CommandId枚舉器值作為cmd參數,檢查cmd參數來確定哪個命令發送到您的啟動屏幕并相應地使用arg值。
下面的 ViewModel 代碼調用SetSplashScreenState方法來傳輸閃屏標簽的“幾乎完成...”字符串。“ SetSplashScreenProgress ”發送當前(80)和最大(100)進度條值。
C#:
public class Form1ViewModel { protected ISplashScreenService SplashScreenService { get { return this.GetService<ISplashScreenService>(); } } public void Show() { SplashScreenService.ShowSplashScreen("SplashScreen1"); SplashScreenService.SetSplashScreenState("Almost done..."); //label text SplashScreenService.SetSplashScreenProgress(80, 100); //progress bar values } }
VB.NET:
Public Class Form1ViewModel Protected ReadOnly Property SplashScreenService() As ISplashScreenService Get Return Me.GetService(Of ISplashScreenService)() End Get End Property Public Sub Show() SplashScreenService.ShowSplashScreen("SplashScreen1") SplashScreenService.SetSplashScreenState("Almost done...") 'label text SplashScreenService.SetSplashScreenProgress(80, 100) 'progress bar values End Sub End Class
SetSplashScreenState方法使用cmd參數的CommandId.MVVMSetState值調用ProcessCommand重寫。SetSplashScreenProgress方法調用ProcessCommand重寫兩次:首先,cmd參數返回 CommandId.SetProgressValue;其次,cmd參數返回 CommandId.SetProgressValue,讀取這些參數值并應用來自arg參數的數據。
C#:
public partial class SplashScreen1 : SplashScreen { public SplashScreen1() { InitializeComponent(); } #region Overrides public override void ProcessCommand(Enum cmd, object arg) { base.ProcessCommand(cmd, arg); DemoProgressSplashScreen.CommandId command = (DemoProgressSplashScreen.CommandId)cmd; //received from the SetSplashScreenState method if(command == DemoProgressSplashScreen.CommandId.MVVMSetState) labelControl2.Text = (string)arg; //two separate values received from the SetSplashScreenProgress method if(command == DemoProgressSplashScreen.CommandId.SetMaxProgressValue) progressBarControl1.Properties.Maximum = (int)arg; if(command == DemoProgressSplashScreen.CommandId.SetProgressValue) progressBarControl1.EditValue = (int)arg; } #endregion }
VB.NET:
Partial Public Class SplashScreen1 Inherits SplashScreen Public Sub New() InitializeComponent() End Sub #Region "Overrides" Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object) MyBase.ProcessCommand(cmd, arg) Dim command As DemoProgressSplashScreen.CommandId = CType(cmd, DemoProgressSplashScreen.CommandId) 'received from the SetSplashScreenState method If command Is DemoProgressSplashScreen.CommandId.MVVMSetState Then labelControl2.Text = DirectCast(arg, String) End If 'two separate values received from the SetSplashScreenProgress method If command Is DemoProgressSplashScreen.CommandId.SetMaxProgressValue Then progressBarControl1.Properties.Maximum = DirectCast(arg, Integer) End If If command Is DemoProgressSplashScreen.CommandId.SetProgressValue Then progressBarControl1.EditValue = DirectCast(arg, Integer) End If End Sub #End Region End Class
下圖展示了結果。
當您更新一個啟動屏幕元素時,請使用上面的示例。否則,由于SetSplashScreenState方法總是返回CommandId.MVVMSetState作為cmd參數,因此無法知道arg數據應該去哪里。對于這種情況,請改用以下方法之一。
- 使用復雜對象作為參數調用SetSplashScreenState方法,該對象應包含枚舉器值和所需的數據。您可以使用System.Tuple結構體、System.Collections.Generic.KeyValuePair對象或object[]數組作為參數。
- 調用使用DevExpress.Utils.MVVM.Services.SplashScreenServiceState對象作為參數的SetSplashScreenState方法,此對象具有Command和State fields字段,使用這些字段可以傳遞所需的數據和相應的枚舉器值。
這些方法如以下代碼所示。首先,聲明一個自定義SplashScreenCommand枚舉器。
C#:
public enum SplashScreenCommand { StateLabelCommand, PercentLabelCommand, ProgressBarCommand }
VB.NET:
Public Enum SplashScreenCommand StateLabelCommand PercentLabelCommand ProgressBarCommand End Enum
這些自定義枚舉器值標記來自SetSplashScreenState方法的不同數據類型。
C#:
public void Show() { SplashScreenService.ShowSplashScreen("SplashScreen1"); //customizing the first label text SplashScreenService.SetSplashScreenState(new SplashScreenServiceState(SplashScreenCommand.StateLabelCommand, "Almost done...")); //customizing the second label text SplashScreenService.SetSplashScreenState(new SplashScreenServiceState(SplashScreenCommand.PercentLabelCommand, "80%")); //sending the current progress bar value object[] customArray = new object[] { SplashScreenCommand.ProgressBarCommand, 80 }; SplashScreenService.SetSplashScreenState(customArray); }
VB.NET:
Public Sub Show() SplashScreenService.ShowSplashScreen("SplashScreen1") 'customizing the first label text SplashScreenService.SetSplashScreenState(New SplashScreenServiceState(SplashScreenCommand.StateLabelCommand, "Almost done...")) 'customizing the second label text SplashScreenService.SetSplashScreenState(New SplashScreenServiceState(SplashScreenCommand.PercentLabelCommand, "80%")) 'sending the current progress bar value Dim customArray() As Object = { SplashScreenCommand.ProgressBarCommand, 80 } SplashScreenService.SetSplashScreenState(customArray) End Sub
由于您的數據現在附帶了相應的枚舉器值,因此可以確定arg參數中存儲了哪些數據并正確使用它。
C#:
public override void ProcessCommand(Enum cmd, object arg) { base.ProcessCommand(cmd, arg); if(cmd.Equals(SplashScreenCommand.StateLabelCommand)) stateLabel.Text = (string)arg; if(cmd.Equals(SplashScreenCommand.PercentLabelCommand)) percentLabel.Text = (string)arg; if(cmd.Equals(SplashScreenCommand.ProgressBarCommand)) progressBarControl1.EditValue = (int)arg; }
VB.NET:
Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object) MyBase.ProcessCommand(cmd, arg) If cmd.Equals(SplashScreenCommand.StateLabelCommand) Then stateLabel.Text = DirectCast(arg, String) End If If cmd.Equals(SplashScreenCommand.PercentLabelCommand) Then percentLabel.Text = DirectCast(arg, String) End If If cmd.Equals(SplashScreenCommand.ProgressBarCommand) Then progressBarControl1.EditValue = DirectCast(arg, Integer) End If End Sub
下圖展示了一個帶有進度條和兩個標簽的啟動畫面,這三個元素使用SetSplashScreenState方法更新。
等待表單
要顯示等待表單,使用相同的ShowSplashScreen和SetSplashScreenState方法。表單有兩個標準的文本塊——標題和描述,因此SetSplashScreenState應該傳遞一個在Wait Form的ProcessCommand方法中解析的雙字符串數組。
C#:
public class MyWaitForm : DevExpress.XtraWaitForm.DemoWaitForm { public override void ProcessCommand(Enum cmd, object arg) { string[] args = arg as string[]; SetCaption(args[0]); SetDescription(args[1]); } } public class MyWaitFormViewModel { protected ISplashScreenService SplashScreenService { get { return this.GetService<ISplashScreenService>(); } } public async System.Threading.Tasks.Task Wait() { SplashScreenService.ShowSplashScreen("MyWaitForm"); SplashScreenService.SetSplashScreenState(new string[] { "Please, wait", "In progress..." }); SplashScreenService.HideSplashScreen(); } }
VB.NET:
Public Class MyWaitForm Inherits DevExpress.XtraWaitForm.DemoWaitForm Public Overrides Sub ProcessCommand(ByVal cmd As [Enum], ByVal arg As Object) Dim args As String() = TryCast(arg, String()) SetCaption(args(0)) SetDescription(args(1)) End Sub End Class Public Class MyWaitFormViewModel Protected ReadOnly Property SplashScreenService As ISplashScreenService Get Return Me.GetService(Of ISplashScreenService)() End Get End Property Public Async Function Wait() As System.Threading.Tasks.Task SplashScreenService.ShowSplashScreen("MyWaitForm") SplashScreenService.SetSplashScreenState(New String() {"Please, wait", "In progress..."}) SplashScreenService.HideSplashScreen() End Function End Class
打開并保存文件對話框服務
這些服務調用允許用戶打開文件并將其保存到本地存儲的對話框。
接口
IOpenFileDialogService , ISaveFileDialogService
管理控件
沒有。
Global Registration
兩項服務均已注冊。
Local Registration
C#:
mvvmContext1.RegisterService(OpenFileDialogService.Create()); mvvmContext1.RegisterService(OpenFileDialogService.Create(mySettings)); mvvmContext1.RegisterService(SaveFileDialogService.Create()); mvvmContext1.RegisterService(SaveFileDialogService.Create(mySettings));
VB.NET:
mvvmContext1.RegisterService(OpenFileDialogService.Create()) mvvmContext1.RegisterService(OpenFileDialogService.Create(mySettings)) mvvmContext1.RegisterService(SaveFileDialogService.Create()) mvvmContext1.RegisterService(SaveFileDialogService.Create(mySettings))
Create() 方法
Create()——創建一個文件對話框服務。
Create(SaveFileDialogServiceOptionsdialogServiceOptions)/Create(OpenFileDialogServiceOptionsdialogServiceOptions)——使用指定的設置創建所需的文件對話框服務(請參閱“公共服務方法”部分中列出的對話框屬性)。
公共服務方式
- ShowDialog(Action<CancelEventArgs> fileOK, string directoryName)——顯示當前對話框服務,如果文件成功打開(保存),則執行fileOK回調,可選的directoryName參數指定啟動對話框文件夾,對于 SaveFileDialogService,第三個字符串 fileName參數也可用,該參數指定保存文件的默認名稱。
- MultiSelect ——一個布爾屬性,指定是否允許用戶同時打開多個文件(僅限 OpenFileDialogService)。
- OverwritePromt —— 一個布爾屬性,指定當您嘗試保存名稱已存在的文件時是否顯示確認消息(僅限 SaveFileDialogService)。
- Title —— 指定對話框標題的字符串值,此屬性和以下所有屬性均繼承自FileDialogService基類。
- DialogStyle——允許您在常規的WinForms和可皮膚的DevExpress對話框之間進行選擇。
- Filter ——指定文件擴展名的字符串值,此對話框支持,這個字符串應該包含過濾器的描述,后面跟著豎條和過濾器模式。下面的代碼演示了一個示例。
C#:
this.Filter = "JPEG Images|*.jpg;*.jpeg|PNG Images|*.png|RAW Image Data|*.raw";
VB.NET:
Me.Filter = "JPEG Images|*.jpg;*.jpeg|PNG Images|*.png|RAW Image Data|*.raw"
- File——返回對話框打開(保存)的文件。
文件夾瀏覽器對話框服務
接口
IFolderBrowserDialogService
管理控件
沒有。
Global Registration
該服務已注冊。
Local Registration
C#:
mvvmContext1.RegisterService(FolderBrowserDialogService.Create()); mvvmContext1.RegisterService(FolderBrowserDialogService.Create(options));
VB.NET:
mvvmContext1.RegisterService(FolderBrowserDialogService.Create()) mvvmContext1.RegisterService(FolderBrowserDialogService.Create(options))
Create() 方法
Create()——創建文件夾瀏覽器對話框服務的新實例。
Create(FolderBrowserDialogServiceOptionsdialogServiceOptions)——使用指定的設置創建文件夾瀏覽器對話框服務的新實例(請參閱“公共服務方法”部分中列出的對話框屬性)。
公共服務方式
- ShowDialog() ——顯示文件夾瀏覽器對話框。
- ShowNewFolderButton—— 一個布爾屬性,指定是否允許用戶在當前層次結構中創建新文件夾。
- StartPath——指定最初選擇的文件夾的字符串屬性。
- RootFolder—— Environment.SpecialFolder類型的屬性,它將層次結構限制為特定文件夾(例如“我的文檔”文件夾)。
- 描述—— 一個字符串屬性,允許您指定對話框的描述。
- DialogStyle——允許您在常規 WinForms 和DevExpress XtraFolderBrowser對話框之間進行選擇。DevExpress 對話框有“Wide”或“Compact”樣式(請參閱XtraFolderBrowserDialog.DialogStyle屬性)。
如何使用服務擴展方法
本節介紹如何使用服務擴展方法的最常見參數。
對象視圖模型
此參數存儲應導航到、在對話框中打開、托管在新 DocumentManager 文檔中等的子ViewModel實例。要創建此類實例,請使用ViewModelSource.Create方法。
C#:
//ViewModelA.cs public class ViewModelA { . . . public static ViewModelA Create() { return ViewModelSource.Create<ViewModelA>(); } } //ViewModelB.cs public class ViewModelB { ViewModelA childViewModel; public ViewModelB() { childViewModel = ViewModelA.Create(); } IDialogService DialogService { get { return this.GetService<IDialogService>(); } } public void ShowDialog() { DialogService.ShowDialog(MessageButton.OK, "This dialog contains View A", "ViewA", childViewModel); } }
VB.NET:
'ViewModelA.vb Public Class ViewModelA . . . Public Shared Function Create() As ViewModelA Return ViewModelSource.Create(Of ViewModelA)() End Function End Class 'ViewModelB.vb Public Class ViewModelB Private childViewModel As ViewModelA Public Sub New() childViewModel = ViewModelA.Create() End Sub Private ReadOnly Property DialogService() As IDialogService Get Return Me.GetService(Of IDialogService)() End Get End Property Public Sub ShowDialog() DialogService.ShowDialog(MessageButton.OK, "This dialog contains View A", "ViewA", childViewModel) End Sub End Class
object parentViewModel
作為SetParentViewModel擴展方法的替代方法,該參數傳遞parent ViewModel的一個實例,使用此參數的擴展方法通常也有Parameter參數。
對象參數
這個參數將特定的對象傳遞給實現ISupportParameter接口的子ViewModels。實現此接口的ViewModels具有Parameter屬性,該屬性會重新計算此參數并將其傳遞回調用方法的位置。
C#:
//child ViewModel public class LoginViewModel: ISupportParameter { . . . public object Parameter { get { // 3. Returns the new parameter value } set { // 2. myParameter object received from the extension method. } } } //parent ViewModel // 1. The extension method is called DialogService.ShowDialog(MessageButton.OK, "This dialog passes the parameter to the child ViewModel", "LoginView", myParameter, this); // 4. myParameter object now has a new value, set within the child ViewModel
VB.NET:
'child ViewModel Public Class LoginViewModel Implements ISupportParameter . . . Public Property Parameter() As Object Get ' 3. Returns the new parameter value End Get Set(ByVal value As Object) ' 2. myParameter object received from the extension method. End Set End Property End Class 'parent ViewModel ' 1. The extension method is called DialogService.ShowDialog(MessageButton.OK, "This dialog passes the parameter to the child ViewModel", "LoginView", myParameter, Me) ' 4. myParameter object now has a new value, set within the child ViewModel
方法變化
共有三種可能的方法參數:viewModel、parentViewModel和parameter。然而,只能有兩種可能的擴展方法組合。
- viewModel:創建一個子 ViewModel(包括其父級和必需的參數),并將該實例傳遞給 View。
- 參數+ parentViewModel:參數被注入到View中并傳遞給為此View創建的子ViewModel。
對于后一種情況,可以使用Framework進行數據注入或者調用以下方法推遲數據注入:
C#:
//postpone all data injection ViewModelInjectionPolicy.DenyViewModelInjection(); //postpone parameter injection ViewModelInjectionPolicy.DenyImmediateParameterInjection(); //postpone parentViewModel injection ViewModelInjectionPolicy.DenyImmediateParentViewModelInjection();
VB.NET:
'postpone all data injection ViewModelInjectionPolicy.DenyViewModelInjection() 'postpone parameter injection ViewModelInjectionPolicy.DenyImmediateParameterInjection() 'postpone parentViewModel injection ViewModelInjectionPolicy.DenyImmediateParentViewModelInjection()