DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress WinForm控件入门指南:WinForms MVVM - 服务(一)

来源:   发布时间:2021-12-23   浏览:1162次

获取工具下载 - DevExpress WinForm v21.2

考虑像显示来自 ViewModel 的通知(例如,消息框)这样的微不足道的任务,作为可视化元素,任何消息框实际上都是视图的一部分。 因此,如果你直接从 ViewModel 显示消息框(定义一个调用 MessageBox.Show() 方法的命令),这个简单的代码将破坏主要的MVVM概念 - ViewModels不能引用Views,并使其无法编写ViewModel的单元测试。为了解决这个困难,DevExpress MVVM 框架实现了服务。

服务是一种 IOC 模式,它删除了 ViewModel 和 View 层之间的任何引用。 在代码中,服务是在 ViewModel 代码中使用的接口,没有任何关于“何时”和“如何”实现该接口的假设。

您可以实现自己的自定义服务以及使用 DevExpress Services,无论您使用什么服务,通用工作流程都保持不变:

  • 在代码中定义服务(如果您使用的是 DevExpress 已经实现的服务,则跳过);
  • 在特定的视图中注册它;
  • 在ViewModel中检索服务并使用其方法。
DevExpress Services

DevExpress MVVM框架已经为大多数常见任务提供了现成的服务——显示消息、弹出窗口、对话框、添加应用程序 UI 管理器文档等。例如,以下 ViewModel 代码通过定义 IMessageBoxService 类型的属性来检索 XtraMessageBoxService。

C#

//ViewModel
public class MyViewModel {
protected IMessageBoxService MessageBoxService {
get { return this.GetService<IMessageBoxService>(); }
}
}

VB.NET

'ViewModel
Public Class MyViewModel
Protected ReadOnly Property MessageBoxService() As IMessageBoxService
Get
Return Me.GetService(Of IMessageBoxService)()
End Get
End Property

重要提示:GetService方法不是线程安全的,不应从后台线程调用。

对于 POCO ViewModel,您可以使用以下故障安全语法,该语法将自动使用 this.GetService 方法或在出现问题时引发异常。

C#

//POCO ViewModel
protected virtual IMessageBoxService MessageBoxService {
get { throw new System.NotImplementedException(); }
}

VB.NET

//POCO ViewModel
Protected Overridable ReadOnly Property MessageBoxService() As IMessageBoxService
Get
Throw New System.NotImplementedException()
End Get
End Property

检索服务后,您可以在 ViewModel 中使用其方法:

C#

public void SayHello() {
MessageBoxService.Show("Hello!");
}

VB.NET

Public Sub SayHello()
MessageBoxService.Show("Hello!")
End Sub

最后,在视图中注册您的服务。 服务要么注册在本地容器中以在单个 View 中使用(本地服务),要么注册到允许您在整个应用程序中使用注册服务的全局静态(单例)服务容器(全局服务)。

C#

//Global service
DevExpress.Mvvm.ServiceContainer.Default.RegisterService(new SomeService());
//Local service
serviceContainer.RegisterService(new SomeFilterService(ModuleType.MyCustomFilter));

VB.NET

'Global service
DevExpress.Mvvm.ServiceContainer.Default.RegisterService(New SomeService())
'Local service
serviceContainer.RegisterService(New SomeFilterService(ModuleType.MyCustomFilter))

当创建 ViewModel 时,服务也可以在运行时在服务容器中注册。

C#

this.ServiceContainer.RegisterService(new Services.AnotherService());

VB.NET

Me.ServiceContainer.RegisterService(New Services.AnotherService())

最后,您可以通过在此级别提供自定义服务实现来覆盖 ViewModel 层次结构中任何级别的父级服务实现。

C#

serviceContainer.RegisterService(new NotImplementedCustomService(ModuleType.MyMainView));

VB.NET

serviceContainer.RegisterService(New NotImplementedCustomService(ModuleType.MyMainView))

使用 MvvmContext 组件,您无需记住这个底层服务容器机制。 该组件的 API 提供了易于使用的方法来注册全局和本地级别的服务。

C#

//Static method that registers the global DevExpress XtraDialogService
MVVMContext.RegisterXtraDialogService();
//Registers the Service1 service in the default service container (global service)
mvvmContext1.RegisterDefaultService(new Service1());
//Registers the local Service1 for use within the current View only
mvvmContext1.RegisterService(new Service2());

VB.NET

'Static method that registers the global DevExpress XtraDialogService
MVVMContext.RegisterXtraDialogService()
'Registers the Service1 service in the default service container (global service)
mvvmContext1.RegisterDefaultService(New Service1())
'Registers the local Service1 for use within the current View only
mvvmContext1.RegisterService(New Service2())

许多随时可用的服务已经在全局静态容器中注册,因此您甚至不需要手动注册它们。 删除 MessageBox 服务演示中的 RegisterMessageBoxService 方法调用,您会注意到该服务仍在工作。

如果需要,您可以重新定义这些服务注册,为此请使用 MVVMContext 类的相应静态 Register... 方法。 例如,XtraMessageBox Service 和 FlyoutMessageBox Service 示例的 ViewModel 与第一个示例的 ViewModel 相同。 所有三个 ViewModel 都检索实现 IMessageBoxService 的服务,但是使用不同的静态 Register... 方法会强制使用不同的服务。

相同的方法允许来自 Dialog Services 组的示例显示不同的对话框,尽管 ViewModel 代码是相同的。

C#

protected IDialogService DialogService {
get { return this.GetService<IDialogService>(); }
}

VB.NET

Protected ReadOnly Property DialogService() As IDialogService
Get
Return Me.GetService(Of IDialogService)()
End Get
End Property

由于注册不同服务的视图代码而调用不同的对话框。

C#

//XtraDialog service
MVVMContext.RegisterXtraDialogService();
//FlyoutDialog service
MVVMContext.RegisterFlyoutDialogService();

VB.NET

'XtraDialog service
MVVMContext.RegisterXtraDialogService()
'FlyoutDialog service
MVVMContext.RegisterFlyoutDialogService()

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


DevExpress技术交流群5:742234706      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress年终放大招!省钱攻略提前奉上,更适合中国区用户!
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/2816.html

相关产品: DevExpress Universal Subscription,

在线
客服
微信
QQ 电话
023-68661681
返回
顶部