翻譯|使用教程|編輯:龔雪|2022-04-14 10:05:30.793|閱讀 281 次
概述:本文主要介紹DevExpress MVVM架構(gòu)下View Model的ViewModelBase,歡迎下載相關(guān)控件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
ViewModelBase類是BindableBase的后代,它繼承了 BindableBase 類的功能(例如 GetProperty、SetProperty 和 RaisePropertyChanged/RaisePropertiesChanged 方法)并提供以下附加功能。
視圖模型可能包含需要訪問數(shù)據(jù)庫(kù)的屬性,在 Visual Studio 設(shè)計(jì)器中工作時(shí),不允許 View Model 連接到數(shù)據(jù)庫(kù),這會(huì)導(dǎo)致設(shè)計(jì)者出現(xiàn)錯(cuò)誤。
在這種情況下,ViewModelBase 類提供了 OnInitializeInDesignMode 和 OnInitializeInRuntime 受保護(hù)的虛擬方法,您可以覆蓋它們以分別初始化運(yùn)行時(shí)和設(shè)計(jì)時(shí)模式的屬性。
C#
public class ViewModel : ViewModelBase { public IEnumerable<Employee> Employees { get { return GetProperty(() => Employees); } private set { SetProperty(() => Employees, value); } } protected override void OnInitializeInDesignMode() { base.OnInitializeInDesignMode(); Employees = new List<Employee>() { new Employee() { Name = "Employee 1" }, }; } protected override void OnInitializeInRuntime() { base.OnInitializeInRuntime(); Employees = DatabaseController.GetEmployees(); } }
VB.NET
Public Class ViewModel Inherits ViewModelBase Public Property Employees As IEnumerable(Of Employee) Get Return GetProperty(Function() Employees) End Get Set(value As IEnumerable(Of Employee)) SetProperty(Function() Employees, value) End Set End Property Protected Overrides Sub OnInitializeInDesignMode() MyBase.OnInitializeInDesignMode() Employees = New List(Of Employee) From { New Employee() With {.Name = "Employee 1"} } End Sub Protected Overrides Sub OnInitializeInRuntime() MyBase.OnInitializeInRuntime() Employees = DatabaseController.GetEmployees() End Sub End Class
ViewModelBase 類實(shí)現(xiàn)了維護(hù)服務(wù)機(jī)制的 ISupportServices 接口,使用 ISupportServices 接口的 ViewModelBase.GetService 方法允許您訪問在視圖中注冊(cè)的服務(wù)。
XAML
<UserControl x:Class="ViewModelBaseSample.View" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:ViewModels="clr-namespace:ViewModelBaseSample.ViewModels" ...> <UserControl.DataContext> <ViewModels:ViewModel/> </UserControl.DataContext> <dxmvvm:Interaction.Behaviors> <dxmvvm:MessageBoxService/> </dxmvvm:Interaction.Behaviors> ... </UserControl>
C#
public class ViewModel : ViewModelBase {
public IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }
}
VB.NET
Public Class ViewModel Inherits ViewModelBase Public ReadOnly Property MessageBoxService As IMessageBoxService Get Return GetService(Of IMessageBoxService)() End Get End Property End Class
從 ViewModelBase 繼承的視圖模型可以通過父子關(guān)系相互關(guān)聯(lián),這是通過在 ViewModelBase 類中實(shí)現(xiàn)的 ISupportParentViewModel 接口實(shí)現(xiàn)的。在這種情況下,子視圖模型可以訪問在主視圖模型中注冊(cè)的服務(wù)。
ViewModelBase 類實(shí)現(xiàn)了 ISupportParameter 接口,該接口可用于將初始數(shù)據(jù)傳遞給視圖模型。
ViewModelBase 類實(shí)現(xiàn) ICustomTypeDescriptor 接口以提供在運(yùn)行時(shí)基于方法(具有 Command 屬性)自動(dòng)創(chuàng)建命令屬性的能力。
創(chuàng)建命令的傳統(tǒng)方法是如下聲明命令屬性。
C#
public class ViewModel : ViewModelBase { public ICommand SaveCommand { get; private set; } public ViewModel() { SaveCommand = new DelegateCommand(Save, CanSave); } public void Save() { //... } public bool CanSave() { //... } }
VB.NET
Public Class ViewModel Inherits ViewModelBase Dim _SaveCommand As ICommand Public ReadOnly Property SaveCommand As ICommand Get Return _SaveCommand End Get End Property Public Sub New() _SaveCommand = New DelegateCommand(AddressOf Save, AddressOf CanSave) End Sub Public Sub Save() '... End Sub Public Function CanSave() As Boolean '... End Function End Class
從 ViewModelBase 派生允許您使用更短的定義,為您希望用作命令的方法設(shè)置 Command 屬性。
C#
public class ViewModel : ViewModelBase { [Command] public void Save() { //... } public bool CanSave() { //... } }
VB.NET
Public Class ViewModel Inherits ViewModelBase <Command> Public Sub Save() '... End Sub Public Function CanSave() As Boolean '... End Function End Class
當(dāng) Command 屬性應(yīng)用于方法時(shí),相應(yīng)的命令將具有名稱:[MethodName]Command”,其中 [MethodName] 是目標(biāo)方法的名稱。 對(duì)于上面的示例,命令名稱將是“SaveCommand”。
XAML
<Button Command="{Binding SaveCommand}"/>
Command 屬性可以應(yīng)用于無參數(shù)方法或具有一個(gè)參數(shù)的方法,您還可以通過設(shè)置命令屬性來自定義命令。
DevExpress WPF擁有120+個(gè)控件和庫(kù),將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實(shí)現(xiàn)。
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)