標(biāo)準(zhǔn)DevExpress服務(wù)
DevExpress Services將ViewModel中的命令傳遞給View中的控件,這允許您在不分離應(yīng)用層的情況下修改UI。
可用服務(wù)
- 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
附加信息
- 如何使用服務(wù)
- 如何使用服務(wù)擴(kuò)展方法
如何使用服務(wù)
1.注冊服務(wù)。
- 本地注冊(服務(wù)僅在視圖中可用):調(diào)用mvvmContext1.RegisterService方法并將 Service 的Create方法之一作為參數(shù)傳遞。DevExpress MVVM 框架自動注冊最常用的服務(wù)——請參閱下面“全局注冊”部分中的注釋。
- 全局注冊(服務(wù)可用于整個應(yīng)用程序):調(diào)用相應(yīng)的靜態(tài)MVVMContext.Register…服務(wù)方法。
- 定義一個ViewModel屬性,返回一個相關(guān)Service接口的對象(例如,如果注冊了WindowedDocumentManagerService,您的屬性應(yīng)該是IDocumentManagerService類型)。
- 使用此屬性可訪問服務(wù)并調(diào)用服務(wù)方法向視圖發(fā)送命令。
示例
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框架自動調(diào)用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枚舉值來確定要創(chuàng)建的服務(wù)類型。
- CreateMessageBoxService() ——創(chuàng)建一個使用標(biāo)準(zhǔn)WinForms消息框的Service。
- CreateXtraMessageBoxService() ——創(chuàng)建一個使用DevExpress XtraMessageBox對象的Service。
- CreateFlyoutMessageBoxService() ——創(chuàng)建一個使用FlyoutDialog對象的服務(wù)。
所有四個方法都有對應(yīng)的重載與第二個IWin32Window所有者參數(shù),此參數(shù)允許指定擁有此服務(wù)的視圖。如果您傳遞的是null而不是owner參數(shù),框架將嘗試找到一個應(yīng)該是Service所有者的適當(dāng)視圖并在大多數(shù)情況下使用活動窗口。
Public Service Members
- ShowMessage ——五個顯示消息框的擴(kuò)展方法。
- MessageBoxFormStyle——允許您訪問消息框表單并修改其外觀設(shè)置。例如,下面的代碼說明了如何將粗體字體樣式應(yīng)用于消息框按鈕。
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框架自動調(diào)用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…'方法都需要一個擁有該服務(wù)的視圖。如果傳遞的是null而不是View,框架會嘗試找到一個合適的窗口(在大多數(shù)情況下,會使用活動窗口)。
- Create(IWin32Window owner, DefaultDialogServiceType type) ——使用DefaultDialogServiceType枚舉值來確定要創(chuàng)建的服務(wù)類型。
- CreateXtraDialogService(IWin32Window所有者)——創(chuàng)建一個顯示可剝皮DevExpress對話框的服務(wù)。
- CreateFlyoutDialogService(IWin32Window所有者)——創(chuàng)建一個顯示flyoutdialog的服務(wù)。
- CreateRibbonDialogService(IWin32Windowowner)——創(chuàng)建一個服務(wù),將帶有嵌入式RibbonControl的RibbonForm顯示為對話框,對話框按鈕顯示為功能區(qū)項目。
- Create(IWin32Window owner, string title, Func<IDialogForm> factoryMethod)——允許您注冊一個Service來管理自定義對話框(實(shí)現(xiàn)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)——接受創(chuàng)建自定義對話框的工廠類。
Public Service Methods
ShowDialog——六種擴(kuò)展方法,顯示具有特定外觀和內(nèi)容的對話框。
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對象替換默認(rèn)對話框按鈕。為此,使用自定義命令的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——允許您訪問對話框并修改其外觀設(shè)置。例如,下面的代碼說明了如何將粗體字體樣式應(yīng)用于彈出對話框按鈕。
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
當(dāng)前對話服務(wù)
允許您管理當(dāng)前可見的對話框。
接口
DevExpress.Mvvm.ICurrentDialogService
注冊
服務(wù)只有在有活動對話框時才存在——您不能注冊CurrentDialogService。
Create()方法
沒有
Public Service Methods
- Close()、Close(MessageResultdialogResult)和Close (UICommanddialogResult) —— 使用給定的DialogResult關(guān)閉對話框,如果結(jié)果是UICommand類型,則調(diào)用相關(guān)的UICommand 。請注意,您只能使用最初傳遞到該方法中的對話框服務(wù)的UICommand之一ShowDialog。
- WindowState——這個屬性允許您改變對話框的窗口狀態(tài)(正常,最小化或最大化)。
當(dāng)前窗口服務(wù)
類似于CurrentDialogService,但是允許您管理當(dāng)前的應(yīng)用程序窗口(形式)。
接口
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(控制容器)——允許您為任何承載作為方法參數(shù)分配的控件的表單注冊服務(wù)。
- 創(chuàng)建(Form currentForm)——為這個表單注冊一個服務(wù)。
- Create(Func<Form> getCurrentForm)——為getCurrentForm方法返回的任何表單注冊一個Service。
公共服務(wù)API
Activate()、Close()、Hide()和Show() ——允許您控制當(dāng)前窗口的可見性。
WindowState ——此屬性允許您更改窗體的窗口狀態(tài)(正常、最小化或最大化)。
窗口服務(wù)
允許您將視圖顯示為獨(dú)立的窗口(形式),并從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"))
本地注冊(模態(tài)窗口)
如果您想把表單顯示為模態(tài)對話框請在注冊前修改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)——創(chuàng)建一個管理xtraform的服務(wù)。
CreateRibbonWindowService(IWin32Window owner, string title = null)——創(chuàng)建一個管理Ribbon窗體的服務(wù)。
CreateFlyoutWindowService(IWin32Window owner, string title = null)——創(chuàng)建一個管理Flyouts的服務(wù)。
Create(IWin32Window owner, DefaultWindowServiceType type, string title = null)——創(chuàng)建一個Service,其類型取決于type參數(shù)。
Create(IWin32Window owner, string title = null, Func<IWindow> factoryMethod = null) ——允許注冊一個服務(wù)來管理自定義表單(實(shí)現(xiàn)IWindowFactory接口的對象)。
Create(IWin32Window owner, string title = null, IWindowFactory factory = null)——接受一個創(chuàng)建自定義窗口的工廠類。
公共服務(wù)方式
- Show(object viewModel)——顯示與此 ViewModel 關(guān)聯(lián)的視圖。
- Show(string documentType, object viewModel)——顯示由目標(biāo) ViewModel 管理的特定視圖。
- Show(string documentType, objectparameter, objectparentViewModel)——允許您將特定參數(shù)傳遞到表單。
- Hide()和Activate()——允許您最小化表單或?qū)⑵渲糜谧钋懊妗?
- Close()——關(guān)閉窗口管理。
DocumentManagerService
提供在MDI(多文檔接口)控件中創(chuàng)建和管理選項卡的方法的本地服務(wù)。
接口
IDocumentManagerService
管理控件
- DocumentManager
- Navigation Frame
- XtraTabControl
- XtraTabbedMdiManager
- Dock Manager
- TabFormControl
Global Registration
由于該服務(wù)管理特定的內(nèi)容提供程序,因此您無法全局注冊該服務(wù)。
Local Registration
C#:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1))
Create()方法
- Create(IDocumentAdapterFactory factory)——創(chuàng)建一個控制特定提供者的服務(wù),提供程序是類的控件或?qū)ο?,派生自IDocumentAdapterFactory接口。factory參數(shù)接受以下類型的對象:
- 所有DocumentManager視圖
- 選項卡MDI管理器
- XtraTabControl
- 導(dǎo)航框架
- Dock Manager
- TabFormControl
- Create(Func<IDocumentAdapter> factoryMethod)——接受一個初始化新工廠對象的factoryMethod函數(shù),這允許您創(chuàng)建自定義工廠(實(shí)現(xiàn)IDocumentAdapterFactory接口的對象)。
Global Registration
由于該服務(wù)管理特定的內(nèi)容提供程序,因此您無法全局注冊該服務(wù)。
Local Registration
C#:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(DocumentManagerService.Create(tabbedView1))
Create()方法
- Create(IDocumentAdapterFactory factory)——創(chuàng)建一個控制特定提供者的服務(wù),提供程序是類的控件或?qū)ο螅缮訧DocumentAdapterFactory接口。factory參數(shù)接受以下類型的對象:
- 所有DocumentManager視圖
- 選項卡MDI管理器
- XtraTabControl
- 導(dǎo)航框架
- Dock Manager
- TabFormControl
- Create(Func<IDocumentAdapter> factoryMethod)——接受一個初始化新工廠對象的factoryMethod函數(shù),這允許您創(chuàng)建自定義工廠(實(shí)現(xiàn)IDocumentAdapterFactory接口的對象)。
公共服務(wù)方式
- Documents——提供對托管內(nèi)容提供者擁有的項(文檔、選項卡、頁面)集合的訪問的屬性。
- ActiveDocument——獲得或設(shè)置一個活躍的項目。
- CreateDocument——創(chuàng)建該內(nèi)容提供商擁有的新項目的三種擴(kuò)展方法。創(chuàng)建的項目的類型取決于提供者類型。對于TabbedView、NativeMdiView視圖和XtraTabbedMdiManager控件,CreateDocument方法創(chuàng)建一個項目,作為選項卡??康教峁┏绦颉榱藙?chuàng)建浮動項,請改用 WindowedDocumentManagerService (見下文)。
窗口文檔管理器服務(wù)
允許您添加承載自定義內(nèi)容的新表單。如果服務(wù)是用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框架自動調(diào)用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參數(shù),框架會嘗試找到一個應(yīng)該是Service所有者的視圖,在大多數(shù)情況下,使用活動窗口。
- Create(IWin32Window owner)——創(chuàng)建具有特定所有者的默認(rèn)類型的Service,默認(rèn)類型是全局注冊的類型。例如,如果您有全局注冊的功能區(qū)表單服務(wù)(RegisterRibbonFormWindowedDocumentManagerService),本地服務(wù)也會顯示功能區(qū)表單,如果沒有注冊全局服務(wù),則默認(rèn)類型為XtraForm。
- Create(IWin32Window owner, DefaultWindowedDocumentManagerServiceType type)——創(chuàng)建一個具有目標(biāo)所有者的本地服務(wù),服務(wù)類型取決于類型參數(shù)。
- CreateXtraFormService(IWin32Window owner)——注冊一個服務(wù),在XtraForms中托管它的項目。
- CreateRibbbonFormService(IWin32Window owner) ——注冊一個服務(wù),在RibbonForms中托管它的項目。
- CreateFlyoutFormService(IWin32Window owner)——注冊一個服務(wù),該服務(wù)在彈出對話框中承載其項目。
- Create(IDocumentAdapterFactory factory) —— 一種擴(kuò)展方法,允許您為 WindowedDocumentManagerService設(shè)置本地內(nèi)容提供程序,使用此方法注冊的服務(wù)將子提供程序項目添加為浮動表單。例如,以下代碼注冊與DocumentManager的TabbedView關(guān)聯(lián)的服務(wù),當(dāng)您調(diào)用該CreateDocument方法時,服務(wù)會將浮動文檔添加到此TabbedView。
C#:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1));
VB.NET:
mvvmContext1.RegisterService(WindowedDocumentManagerService.Create(tabbedView1))
下面的對象實(shí)現(xiàn)了IDocumentAdapterFactory接口,并且可以作為參數(shù)傳遞給這個方法:
- DocumentManager組件的TabbedView和NativeMdiView視圖
- XtraTabbedMdiManager
XtraTabControl和NavigationFrame子項目總是??康?,不能將這些控件用作工廠參數(shù)。
Create(Func<Form> factoryMethod, IWin32Window owner) ——允許您創(chuàng)建自定義工廠(實(shí)現(xiàn)IDocumentAdapterFactory接口的對象)。
公共服務(wù)方式
- Documents——提供對此服務(wù)管理的項集合的訪問的屬性。
- ActiveDocument——獲取或設(shè)置活動項。
- CreateDocument——創(chuàng)建新項的三個擴(kuò)展方法,根據(jù)注冊的不同,項目是一個獨(dú)立的表單/XtraForm/RibbonForm或浮動面板由DocumentManager/XtraTabbedMdiManager擁有。
導(dǎo)航服務(wù)
該服務(wù)允許您在NavigationFrame控件中從一個視圖導(dǎo)航到另一個視圖,并將應(yīng)用程序視圖作為托管控件中的頁面打開(例如,作為TabbedView選項卡)。
接口
INavigationService
管理控件
- 導(dǎo)航框架
- 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)——允許您為此服務(wù)設(shè)置本地內(nèi)容提供者的擴(kuò)展方法,當(dāng)使用此方法創(chuàng)建時,服務(wù)將創(chuàng)建新項作為提供者的子項。
公共服務(wù)方式
與DocumentManagerService中相同的命令可用,加上以下導(dǎo)航API:
- BackNavigationMode——允許您指定當(dāng)用戶按下“返回”按鈕時屏幕上出現(xiàn)的模塊:前一個模塊還是根模塊。
- GoBack, GoForward ——導(dǎo)航到先前查看的模塊或放棄此導(dǎo)航。
- CanGoBack, CanGoForward ——返回是否可以在給定方向上導(dǎo)航。
- Navigate ——導(dǎo)航到目標(biāo)視圖,其名稱作為字符串參數(shù)傳遞給此方法。
DispatcherService
允許您使用dispatcher在ViewModel中執(zhí)行操作。
接口
管理控件
沒有。
Global Registration
此服務(wù)已注冊。
Local Registration
C#:
mvvmContext1.RegisterService(DispatcherService.Create());
VB.NET:
mvvmContext1.RegisterService(DispatcherService.Create())
Create()方法
- Create()——創(chuàng)建一個新的Service實(shí)例。
公共服務(wù)方式
BeginInvoke——異步執(zhí)行指定的委托。
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
通知服務(wù)
顯示傳統(tǒng)的警報窗口和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)——創(chuàng)建一個使用目標(biāo)管理器顯示通知的服務(wù),接受ToastNotificationsManager和AlertControl類實(shí)例作為參數(shù)。
公共服務(wù)方式
- CreatePredefinedNotification(string header, string body, string body2, object image = null)——創(chuàng)建帶有圖像、標(biāo)題文本字符串和兩個常規(guī)正文文本字符串的通知。注意,這個方法創(chuàng)建了一個通知,但沒有顯示它——要使它可見,請調(diào)用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 操作系統(tǒng)設(shè)置禁用 toast 通知),則該方法會在非UI線程中異步引發(fā)異常,此異常不會影響UI線程。要處理這些異常并響應(yīng)通知顯示失敗,請ShowAsync使用塊包裝方法的調(diào)用try..catch。
- CreateCustomNotification(object viewModel)——創(chuàng)建一個帶有 ViewModel 的通知,ViewModel 參數(shù)需要一個實(shí)現(xiàn)DevExpress.Utils.MVVM.Services.INotificationInfo接口的類的實(shí)例。該界面公開一張圖像和三個字符串屬性,允許您通知設(shè)置圖標(biāo)、標(biāo)題文本字符串和兩個常規(guī)文本字符串。下面的代碼說明了一個示例。
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方法創(chuàng)建一個通知,但不顯示它,要顯示通知,調(diào)用通知的' show '和' Hide '方法。
啟動畫面服務(wù)
此服務(wù)允許您顯示啟動屏幕和等待表單,表明應(yīng)用程序正忙。
接口
管理控件
Splash Screen Manager
Global Registration
此服務(wù)已注冊。
Local Registration
C#:
mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager));
VB.NET:
mvvmContext.RegisterService(SplashScreenService.Create(splashScreenManager))
Create()方法
- Create(ISplashScreenServiceProvider serviceProvider)——創(chuàng)建一個管理目標(biāo)啟動屏幕管理器的服務(wù)。
- Create(ISplashScreenServiceProvider serviceProvider, DefaultBoolean throwExceptions) ——創(chuàng)建一個服務(wù),該服務(wù)管理目標(biāo)啟動屏幕管理器,并在發(fā)生錯誤時拋出異常。
公共服務(wù)方式
ShowSplashScreen(string documentType)—— 顯示啟動屏幕或特定類型的等待表單。“documentType”參數(shù)是從SplashScreen類派生的 ViewModel 的名稱,表示需要顯示的啟動屏幕。如果傳遞null作為參數(shù),則會創(chuàng)建由DevExpress設(shè)計的默認(rèn)啟動屏幕。
要顯示Fluent Splash Screen或Overlay Form,請將相應(yīng)的字符串 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) ——將自定義數(shù)據(jù)注入當(dāng)前可見的啟動畫面或等待表單的方法,SetSplashScreenProgress方法更新啟動屏幕進(jìn)度條,SetSplashScreenState發(fā)送任何其他類型的數(shù)據(jù)(例如,啟動屏幕標(biāo)簽的字符串?dāng)?shù)據(jù))。
啟動畫面
啟動畫面可以利用這兩種方法,要接收和使用注入的數(shù)據(jù),請使用啟動屏幕管理器的智能標(biāo)簽菜單添加新的啟動屏幕。啟動屏幕的代碼包含“覆蓋”區(qū)域:覆蓋其SplashFormBase.ProcessCommand方法來解析數(shù)據(jù)。
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方法還可以將數(shù)據(jù)發(fā)送到啟動屏幕和等待表單。為此,請使用簡單對象(字符串、數(shù)值等)作為方法參數(shù),執(zhí)行此操作時,SplashFormBase.ProcessCommand方法將接收這些簡單對象作為arg參數(shù),并接收DemoProgressSplashScreen.CommandId枚舉器值作為cmd參數(shù),檢查cmd參數(shù)來確定哪個命令發(fā)送到您的啟動屏幕并相應(yīng)地使用arg值。
下面的 ViewModel 代碼調(diào)用SetSplashScreenState方法來傳輸閃屏標(biāo)簽的“幾乎完成...”字符串。“ SetSplashScreenProgress ”發(fā)送當(dāng)前(80)和最大(100)進(jìn)度條值。
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參數(shù)的CommandId.MVVMSetState值調(diào)用ProcessCommand重寫。SetSplashScreenProgress方法調(diào)用ProcessCommand重寫兩次:首先,cmd參數(shù)返回 CommandId.SetProgressValue;其次,cmd參數(shù)返回 CommandId.SetProgressValue,讀取這些參數(shù)值并應(yīng)用來自arg參數(shù)的數(shù)據(jù)。
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
下圖展示了結(jié)果。
當(dāng)您更新一個啟動屏幕元素時,請使用上面的示例。否則,由于SetSplashScreenState方法總是返回CommandId.MVVMSetState作為cmd參數(shù),因此無法知道arg數(shù)據(jù)應(yīng)該去哪里。對于這種情況,請改用以下方法之一。
- 使用復(fù)雜對象作為參數(shù)調(diào)用SetSplashScreenState方法,該對象應(yīng)包含枚舉器值和所需的數(shù)據(jù)。您可以使用System.Tuple結(jié)構(gòu)體、System.Collections.Generic.KeyValuePair對象或object[]數(shù)組作為參數(shù)。
- 調(diào)用使用DevExpress.Utils.MVVM.Services.SplashScreenServiceState對象作為參數(shù)的SetSplashScreenState方法,此對象具有Command和State fields字段,使用這些字段可以傳遞所需的數(shù)據(jù)和相應(yīng)的枚舉器值。
這些方法如以下代碼所示。首先,聲明一個自定義SplashScreenCommand枚舉器。
C#:
public enum SplashScreenCommand { StateLabelCommand, PercentLabelCommand, ProgressBarCommand }
VB.NET:
Public Enum SplashScreenCommand StateLabelCommand PercentLabelCommand ProgressBarCommand End Enum
這些自定義枚舉器值標(biāo)記來自SetSplashScreenState方法的不同數(shù)據(jù)類型。
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
由于您的數(shù)據(jù)現(xiàn)在附帶了相應(yīng)的枚舉器值,因此可以確定arg參數(shù)中存儲了哪些數(shù)據(jù)并正確使用它。
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
下圖展示了一個帶有進(jìn)度條和兩個標(biāo)簽的啟動畫面,這三個元素使用SetSplashScreenState方法更新。
等待表單
要顯示等待表單,使用相同的ShowSplashScreen和SetSplashScreenState方法。表單有兩個標(biāo)準(zhǔn)的文本塊——標(biāo)題和描述,因此SetSplashScreenState應(yīng)該傳遞一個在Wait Form的ProcessCommand方法中解析的雙字符串?dāng)?shù)組。
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
打開并保存文件對話框服務(wù)
這些服務(wù)調(diào)用允許用戶打開文件并將其保存到本地存儲的對話框。
接口
IOpenFileDialogService , ISaveFileDialogService
管理控件
沒有。
Global Registration
兩項服務(wù)均已注冊。
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()——創(chuàng)建一個文件對話框服務(wù)。
Create(SaveFileDialogServiceOptionsdialogServiceOptions)/Create(OpenFileDialogServiceOptionsdialogServiceOptions)——使用指定的設(shè)置創(chuàng)建所需的文件對話框服務(wù)(請參閱“公共服務(wù)方法”部分中列出的對話框?qū)傩裕?
公共服務(wù)方式
- ShowDialog(Action<CancelEventArgs> fileOK, string directoryName)——顯示當(dāng)前對話框服務(wù),如果文件成功打開(保存),則執(zhí)行fileOK回調(diào),可選的directoryName參數(shù)指定啟動對話框文件夾,對于 SaveFileDialogService,第三個字符串 fileName參數(shù)也可用,該參數(shù)指定保存文件的默認(rèn)名稱。
- MultiSelect ——一個布爾屬性,指定是否允許用戶同時打開多個文件(僅限 OpenFileDialogService)。
- OverwritePromt —— 一個布爾屬性,指定當(dāng)您嘗試保存名稱已存在的文件時是否顯示確認(rèn)消息(僅限 SaveFileDialogService)。
- Title —— 指定對話框標(biāo)題的字符串值,此屬性和以下所有屬性均繼承自FileDialogService基類。
- DialogStyle——允許您在常規(guī)的WinForms和可皮膚的DevExpress對話框之間進(jìn)行選擇。
- Filter ——指定文件擴(kuò)展名的字符串值,此對話框支持,這個字符串應(yīng)該包含過濾器的描述,后面跟著豎條和過濾器模式。下面的代碼演示了一個示例。
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——返回對話框打開(保存)的文件。
文件夾瀏覽器對話框服務(wù)
接口
IFolderBrowserDialogService
管理控件
沒有。
Global Registration
該服務(wù)已注冊。
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()——創(chuàng)建文件夾瀏覽器對話框服務(wù)的新實(shí)例。
Create(FolderBrowserDialogServiceOptionsdialogServiceOptions)——使用指定的設(shè)置創(chuàng)建文件夾瀏覽器對話框服務(wù)的新實(shí)例(請參閱“公共服務(wù)方法”部分中列出的對話框?qū)傩裕?
公共服務(wù)方式
- ShowDialog() ——顯示文件夾瀏覽器對話框。
- ShowNewFolderButton—— 一個布爾屬性,指定是否允許用戶在當(dāng)前層次結(jié)構(gòu)中創(chuàng)建新文件夾。
- StartPath——指定最初選擇的文件夾的字符串屬性。
- RootFolder—— Environment.SpecialFolder類型的屬性,它將層次結(jié)構(gòu)限制為特定文件夾(例如“我的文檔”文件夾)。
- 描述—— 一個字符串屬性,允許您指定對話框的描述。
- DialogStyle——允許您在常規(guī) WinForms 和DevExpress XtraFolderBrowser對話框之間進(jìn)行選擇。DevExpress 對話框有“Wide”或“Compact”樣式(請參閱XtraFolderBrowserDialog.DialogStyle屬性)。
如何使用服務(wù)擴(kuò)展方法
本節(jié)介紹如何使用服務(wù)擴(kuò)展方法的最常見參數(shù)。
對象視圖模型
此參數(shù)存儲應(yīng)導(dǎo)航到、在對話框中打開、托管在新 DocumentManager 文檔中等的子ViewModel實(shí)例。要創(chuàng)建此類實(shí)例,請使用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擴(kuò)展方法的替代方法,該參數(shù)傳遞parent ViewModel的一個實(shí)例,使用此參數(shù)的擴(kuò)展方法通常也有Parameter參數(shù)。
對象參數(shù)
這個參數(shù)將特定的對象傳遞給實(shí)現(xiàn)ISupportParameter接口的子ViewModels。實(shí)現(xiàn)此接口的ViewModels具有Parameter屬性,該屬性會重新計算此參數(shù)并將其傳遞回調(diào)用方法的位置。
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
方法變化
共有三種可能的方法參數(shù):viewModel、parentViewModel和parameter。然而,只能有兩種可能的擴(kuò)展方法組合。
- viewModel:創(chuàng)建一個子 ViewModel(包括其父級和必需的參數(shù)),并將該實(shí)例傳遞給 View。
- 參數(shù)+ parentViewModel:參數(shù)被注入到View中并傳遞給為此View創(chuàng)建的子ViewModel。
對于后一種情況,可以使用Framework進(jìn)行數(shù)據(jù)注入或者調(diào)用以下方法推遲數(shù)據(jù)注入:
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()