翻譯|使用教程|編輯:龔雪|2021-12-30 10:18:05.220|閱讀 272 次
概述:DevExpress WinForm創(chuàng)建的應(yīng)用程序可利用MVVM設(shè)計(jì)模式,本文主要介紹服務(wù)功能,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
獲取工具下載 - DevExpress WinForm v21.2
考慮像顯示來(lái)自 ViewModel 的通知(例如,消息框)這樣的微不足道的任務(wù),作為可視化元素,任何消息框?qū)嶋H上都是視圖的一部分。 因此,如果你直接從 ViewModel 顯示消息框(定義一個(gè)調(diào)用 MessageBox.Show() 方法的命令),這個(gè)簡(jiǎn)單的代碼將破壞主要的MVVM概念 - ViewModels不能引用Views,并使其無(wú)法編寫ViewModel的單元測(cè)試。為了解決這個(gè)困難,DevExpress MVVM 框架實(shí)現(xiàn)了服務(wù)。
服務(wù)是一種 IOC 模式,它刪除了 ViewModel 和 View 層之間的任何引用。 在代碼中,服務(wù)是在 ViewModel 代碼中使用的接口,沒(méi)有任何關(guān)于“何時(shí)”和“如何”實(shí)現(xiàn)該接口的假設(shè)。
您可以實(shí)現(xiàn)自己的自定義服務(wù)以及使用 DevExpress Services,無(wú)論您使用什么服務(wù),通用工作流程都保持不變:
對(duì)于自定義服務(wù),您首先需要在單獨(dú)類中的某處實(shí)現(xiàn)此服務(wù),例如應(yīng)用程序具有帶有 Notify 方法的自定義接口 IMyNotificationService。
C#
//View public interface IMyNotificationService { void Notify(string message); }
VB.NET
'View Public Interface IMyNotificationService Sub Notify(ByVal message As String) End Interface
然后,實(shí)現(xiàn)此接口的自定義服務(wù) CustomService1 將如下所示。
C#
//Service1.cs class CustomService1 : IMyNotificationService { void IMyNotificationService.Notify(string message) { System.Windows.Forms.MessageBox.Show(message, "Service1"); } }
VB.NET
'Service1.vb Friend Class CustomService1 Implements IMyNotificationService Private Sub IMyNotificationService_Notify(ByVal message As String) Implements IMyNotificationService.Notify System.Windows.Forms.MessageBox.Show(message, "Service1") End Sub End Class
作為變體,創(chuàng)建另一個(gè)實(shí)現(xiàn)相同接口但使用不同方法重載的服務(wù)。
C#
//Service2.cs class CustomService2 : IMyNotificationService { void IMyNotificationService.Notify(string message) { System.Windows.Forms.MessageBox.Show(message, "Service2"); } }
VB.NET
'Service2.vb Friend Class CustomService2 Implements IMyNotificationService Private Sub IMyNotificationService_Notify(ByVal message As String) Implements IMyNotificationService.Notify System.Windows.Forms.MessageBox.Show(message, "Service2") End Sub End Class
在 ViewModel 代碼中檢索自定義服務(wù)的屬性將如下所示。
C#
//ViewModel public virtual IMyNotificationService Service { get { throw new NotImplementedException(); } } public virtual IMyNotificationService AnotherService { get { throw new NotImplementedException(); } }
VB.NET
'ViewModel Public Overridable ReadOnly Property Service() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property Public Overridable ReadOnly Property AnotherService() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property
這是可以綁定到 UI 元素(例如,按鈕)的 DoSomething 方法,它將顯示具有相同文本的兩條消息。
C#
//ViewModel public void DoSomething() { Service.Notify("Hello"); AnotherService.Notify("Hello"); }
VB.NET
'ViewModel Public Sub DoSomething() Service.Notify("Hello") AnotherService.Notify("Hello") End Sub
最后,在視圖中注冊(cè)您的自定義服務(wù)。 由于這些是您自己的自定義服務(wù),因此不存在用于注冊(cè)這些服務(wù)的預(yù)定義靜態(tài) MVVMContext 方法。 相反,調(diào)用本地 MvvmContext 實(shí)例的 RegisterService 方法。
C#
//View mvvmContext1.RegisterService(new CustomService1()); mvvmContext1.RegisterService(new CustomService2());
VB.NET
'View mvvmContext1.RegisterService(New CustomService1()) mvvmContext1.RegisterService(New CustomService2())
提示:注冊(cè)后,服務(wù)在分層樹中占據(jù)特定位置。每當(dāng)框架需要服務(wù)時(shí),它就會(huì)從樹的底部開始尋找,向上移動(dòng)直到找到合適的服務(wù)。前面有提到很多現(xiàn)成的服務(wù)已經(jīng)在靜態(tài)容器中注冊(cè)了,這些服務(wù)位于層次結(jié)構(gòu)的最頂層,如果框架沒(méi)有在較低級(jí)別找到任何自定義服務(wù),則會(huì)使用這些服務(wù)。如果這兩個(gè)默認(rèn)服務(wù)都不存在,則會(huì)發(fā)生異常。在此示例中,兩個(gè)自定義服務(wù)都注冊(cè)在同一層次結(jié)構(gòu)級(jí)別上。由于這兩個(gè)服務(wù)實(shí)現(xiàn)了相同的 IMyNotificationService 服務(wù),因此在調(diào)用 Service 或 AnotherService 對(duì)象的 Notify 方法時(shí),它們都被視為合適的服務(wù)。但是 CustomService2 是最后注冊(cè)的,因此它更靠近層次結(jié)構(gòu)底部,并且總是首先被框架“找到”。您可以欺騙此機(jī)制并使用 RegisterDefaultService 方法注冊(cè) CustomService2,這將在層次結(jié)構(gòu)頂部的靜態(tài)容器中注冊(cè)您的 CustomService2,并使 CustomSerive1 成為最底層的服務(wù)。之后,框架將始終選擇 CustomService1。
為了解決這個(gè)問(wèn)題,您可以定義服務(wù)密鑰。key是標(biāo)記特定服務(wù)的字符串標(biāo)識(shí)符,對(duì)于 POCO ViewModel,您可以將服務(wù)密鑰設(shè)置為 [ServiceProperty] 屬性的參數(shù)。
C#
[ServiceProperty(Key="Service1")] public virtual IMyNotificationService Service { get { throw new NotImplementedException(); } } [ServiceProperty(Key = "Service2")] public virtual IMyNotificationService AnotherService { get { throw new NotImplementedException(); } }
VB.NET
<ServiceProperty(Key:="Service1")> Public Overridable ReadOnly Property Service() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property <ServiceProperty(Key := "Service2")> Public Overridable ReadOnly Property AnotherService() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property
對(duì)于非 POCO ViewModel,可以將服務(wù)密鑰設(shè)置為 GetService 擴(kuò)展方法的參數(shù)。
C#
public IServiceInterface MyService { get { return this.GetService<IServiceInterface >("MyServiceKey"); } }
VB.NET
Public ReadOnly Property MyService() As IServiceInterface Get Return Me.GetService(Of IServiceInterface )("MyServiceKey") End Get End Property
現(xiàn)在,您必須使用這些唯一密鑰注冊(cè)您的自定義服務(wù),所有 Register 方法都有相應(yīng)的重載來(lái)做到這一點(diǎn)。
C#
mvvmContext1.RegisterService("Service1", new CustomService1()); mvvmContext1.RegisterService("Service2", new CustomService2());
VB.NET
mvvmContext1.RegisterService("Service1", New CustomService1()) mvvmContext1.RegisterService("Service2", New CustomService2())
因此,當(dāng)您調(diào)用 Notify 方法時(shí),框架將不會(huì)混淆應(yīng)該使用哪個(gè) IMyNotificationService 服務(wù)實(shí)現(xiàn)。 相反它將采用標(biāo)有您的自定義密鑰的確切服務(wù),例如,AnotherService 屬性將尋找標(biāo)有“Service2”鍵的服務(wù),并找到已注冊(cè)的 CustomService2 服務(wù)。 與 Service 屬性相同,它將使用 CustomService1 服務(wù),因?yàn)樗鼧?biāo)有“Service1”鍵。
如果您現(xiàn)在測(cè)試應(yīng)用程序,您將看到兩個(gè)消息框現(xiàn)在顯示不同的標(biāo)題,因?yàn)樗鼈兪怯刹煌?wù)的方法顯示的。
DevExpress WinForm擁有180+組件和UI庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢
DevExpress技術(shù)交流群5:742234706 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)