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
如何使用服务
- 注册服务。
- 本地注册(Service 仅在 View 中可用):调用 mvvmContext1.RegisterService 方法并将 Service 的 Create 方法之一作为参数传递,DevExpress MVVM 框架自动注册最常用的服务。
- 全局注册(整个应用可用的Service):调用对应的静态MVVMContext.Register...Service方法。
- 定义一个 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
允许您显示消息框和浮出控件。
接口
托管控件
- System.Windows.Forms.MessageBox
- XtraMessageBox
- FlyoutDialog
全局注册
C#
MVVMContext.RegisterMessageBoxService();
MVVMContext.RegisterXtraMessageBoxService();
MVVMContext.RegisterFlyoutMessageBoxService();
VB.NET
MVVMContext.RegisterMessageBoxService()
MVVMContext.RegisterXtraMessageBoxService()
MVVMContext.RegisterFlyoutMessageBoxService()
DevExpress MVVM 框架自动调用 RegisterXtraMessageBoxService 方法。
本地注册
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 消息框的服务。
- CreateXtraMessageBoxService() - 创建一个使用 DevExpress XtraMessageBox 对象的服务。
- CreateFlyoutMessageBoxService() - 创建一个使用 FlyoutDialog 对象的服务。
所有四个方法都具有与第二个 IWin32Window 所有者参数对应的重载,此参数允许您指定拥有此服务的视图。 如果您传递 null 而不是 owner 参数,则框架会尝试找到应该是服务所有者的适当视图。 在大多数情况下,使用激活窗口。
公共服务成员
- 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
DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
DevExpress技术交流群5:742234706 欢迎一起进群讨论